如何使用 OCMock 初始化带有存根值的对象
如何对 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?
Ho do I stub a method used in the init method?
The related methods in my class:
- (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;
}
My test:
- (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");
}
My result:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'OCMockObject[AdViewController]: unexpected method invoked: smallAdRight '
So how will I access the AdViewController in the OCMockObject?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用
mockForClass:
方法,您将需要为模拟类中调用的每个方法提供存根实现。在您的第一次测试中包括使用 [controllersmallAdRight] 进行的调用。相反,您可以使用
niceMockForClass:
方法,该方法将忽略任何未模拟的消息。另一种替代方法是实例化您的
AdViewController
,然后使用partialMockForObject:
方法为其创建部分模拟。这样控制器类的内部将完成主要部分的工作。只是,您是否正在尝试测试 AdViewController 或使用它的类?看来您正在尝试模拟整个班级,然后测试它是否仍然表现正常。如果您想测试
AdViewController
在注入某些值时是否按预期运行,那么您最好的选择很可能是partialMockForObject:
方法:If you use the
mockForClass:
method you will need to provided stubbed implementations for each and every method that are called in the mocked class. including your call into it with [controller smallAdRight] in your first test.Instead you can use the
niceMockForClass:
method which will just ignore any messages which are not mocked.Another alternative is to instantiate your
AdViewController
and then create a partial mock for it using thepartialMockForObject:
method. This way the internals of the controller class will do the main part of the work.Just a though... are you trying to test the AdViewController or a class which uses it? It appears that you are trying to mock the entire class and then test if it still behaves normally. If you want to test that
AdViewController
behaves as expected when certain values are injected then your best option is most likely thepartialMockForObject:
method: