- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如何 stub init 方法中使用的方法?
我类的相关方法:
- (id)init
{
self = [super init];
if (self) {
if (self.adConfigurationType == AdConfigurationTypeDouble) {
[self configureForDoubleConfiguration];
}
else {
[self configureForSingleConfiguration];
}
}
return self;
}
- (AdConfigurationType)adConfigurationType
{
if (adConfigurationType == NSNotFound) {
if ((random()%2)==1) {
adConfigurationType = AdConfigurationTypeSingle;
}
else {
adConfigurationType = AdConfigurationTypeDouble;
}
}
return adConfigurationType;
}
我的测试:
- (void)testDoubleConfigurationLayout
{
id mockController = [OCMockObject mockForClass:[AdViewController class]];
AdConfigurationType type = AdConfigurationTypeDouble;
[[[mockController stub] andReturnValue:OCMOCK_VALUE(type)] adConfigurationType];
id controller = [mockController init];
STAssertNotNil([controller smallAdRight], @"Expected a value here");
STAssertNotNil([controller smallAdRight], @"Expected a value here");
STAssertNil([controller largeAd], @"Expected nil here");
}
我的结果:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“OCMockObject[AdViewController]:调用了意外的方法:smallAdRight”
那么我将如何访问 OCMockObject 中的 AdViewController?
最佳答案
如果您使用 mockForClass:
方法,您需要为模拟类中调用的每个方法提供 stub 实现。在你的第一个测试中包括你用 [controller smallAdRight] 调用它。
相反,您可以使用 niceMockForClass:
方法,该方法将忽略任何未模拟的消息。
另一种方法是实例化您的 AdViewController
,然后使用 partialMockForObject:
方法为其创建部分模拟。这样 Controller 类的内部将完成主要部分的工作。
不过……您是要测试 AdViewController 还是使用它的类?看来您正在尝试模拟整个类(class),然后测试它是否仍然表现正常。如果您想测试 AdViewController
在注入(inject)某些值时是否按预期运行,那么最好的选择很可能是 partialMockForObject:
方法:
- (void)testDoubleConfigurationLayout {
AdViewController *controller = [AdViewController alloc];
id mock = [OCMockObject partialMockForObject:controller];
AdConfigurationType type = AdConfigurationTypeDouble;
[[[mock stub] andReturnValue:OCMOCK_VALUE(type)] adConfigurationType];
// You'll want to call init after the object have been stubbed
[controller init]
STAssertNotNil([controller smallAdRight], @"Expected a value here");
STAssertNotNil([controller smallAdRight], @"Expected a value here");
STAssertNil([controller largeAd], @"Expected nil here");
}
关于objective-c - 如何使用 OCMock 初始化具有 stub 值的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7413920/
我有以下 Spring 代码要使用 Spock 进行测试: @Service @RequiredArgsConstructor public class MyService { private f
package main import ( "fmt" "github.com/hyperledger/fabric/core/chaincode/shim" pb "gith
我刚开始使用Wiremock,但对 stub 有疑问。 从文档看来,您似乎可以在映射下使用JSON文件,也可以在Java代码中使用代码stubFor(get(urlEqualTo(...。但是,我发现
我有以下要测试的对象: public class MyObject { @Inject Downloader downloader; public List readFi
我看到它被使用过很多次,但从未真正停下来质疑过它。现在我想知道 stub 和 stub 之间是否有区别! 有吗?还是历史原因? stub !意思是它 stub 一次?并返回到正常的方法调用? 最佳答案
在 Jasmine 中,如何创建一个纯 stub ,其中所有方法都已 stub 并返回未定义? 最佳答案 我认为没有任何现成的东西可以做到这一点,但您可以创建自己的。 describe('Stub a
两个类。父级:B。子级:A。A.method1() 覆盖 B 的。 public class B { protected boolean method1(){...}; } public cl
我有一个函数依赖于另一个函数,而不是测试依赖性我只想测试该依赖函数的特定结果。但是,当我对函数进行 stub 时,什么也没有发生,返回结果就好像我一开始就没有对函数进行 stub 一样。 示例代码:
这是要测试的代码: const AWS = require('aws-sdk'); const { APPLICATIONS, NOTIFICATION_FREQUENCIES } = req
背景 Any client socket program(C) over TCP/IP looks like, /* Socket creation */ sockfd = socket(AF_I
我正在尝试使用 stub 提供程序(我从 this 问题的答案中得到)和 stub 验证器来实现一个简单的同步适配器。对于身份验证,我使用了基本的 sync adapter example由谷歌提供。
与在测试点使用模拟框架(如 Rhino Mocks)相比,是否存在手动创建 stub 类型更有利的情况。 我们在项目中采用了这两种方法。当我查看一长串对象的 stub 版本时,我的直觉是它会增加维护开
我想 stub doSomething 来回调错误。但是,我只希望它在第一次响应时回调并出现错误。我想在第一次调用后恢复 stub 为了 stub 第一个调用,我可以这样做: var stub = s
我有一个 TimeMachine 类,它为我提供当前日期/时间值。该类看起来像这样: public class TimeMachine { public virtual DateTime Ge
如果我有一个 Rhino Mock 对象,它已经像这样声明了一个 stub 调用: mockEmploymentService.Stub(x => x.GetEmployment(999)).Retu
通常使用 Mockito,如果你 stub 一个被多次调用的方法,你会这样做 Mockito .doReturn(0) .doReturn(1) .doReturn(2)
逻辑 public class Logic { String date = (LocalDateTime.now()).format(DateTimeFormatter.ofPattern("yyyy
我想达到的目的 At the time of compilation, the compiler knew the function call was valid because you includ
这可能是一个简单的问题,但我无法缩短它。 我正在测试我的一个类,ClassToTest。在生产中,它将对第三方库对象(ThirdPartyClass 的实例)执行操作。 我想用 stub 模拟那个类。
我是 js 单元测试的新手,对使用 withArgs 进行 stub 有疑问。 我有一些名为“create”的通用外部函数,我只想为某种参数和原始“create”的其他返回值 stub 它。例如: s
我是一名优秀的程序员,十分优秀!