我如何存根 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 呢?