我是新來OCMockObjects並試圖嘲弄ACAccount類的實例方法:OCMockObject嘲諷的實例方法
-(NSArray *)accountsWithAccountType:(ACAccountType *)accountType;
我寫了這個代碼測試類並初始化mockObject:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
id mockAccountStore = [OCMockObject partialMockForObject:accountStore];
[[[mockAccountStore stub] andReturn:@[@"someArray"]] accountsWithAccountType:[OCMArg any]];
//call the test method
[myClassInstance methodToTest];
在myClass methodToTest看起來像這樣:
-(void) methodToTest
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType* accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSArray* expectedArray = [accountStore accountsWithAccountType:accountType];
//Getting nil value to Array rather than the returned value in stub
}
任何想法我在這裏做錯了什麼。謝謝。
我喜歡這個主意,但它確實意味着你正在擴大範圍的ACAccountStore對象。雖然不是很大的交易。 –
是的,我認爲第二種方法基本上等同於OP的原始代碼 - 它只是將初始化移動到您可以在測試時攔截的方法。 –