OCMock 2
id mock = [OCMockObject mockForClass:[ExtClass class]];
// We stub someMethod
BOOL returnedValue = YES;
[[[mock stub] andReturnValue:OCMOCK_VALUE(returnedValue)] someMethod];
// Here we stub the alloc class method **
[[[mock stub] andReturn:mock] alloc];
// And we stub initWithParam: passing the param we will pass to the method to test
NSString *param = @"someParam";
[[[mock stub] andReturn:mock] initWithParam:param];
// Here we call the method to test and we would do an assertion of its returned value...
[YourClassToTest foo:param];
OCMock3
// Parameter
NSURL *url = [NSURL URLWithString:@"http://testURL.com"];
// Set up the class to mock `alloc` and `init...`
id mockController = OCMClassMock([WebAuthViewController class]);
OCMStub([mockController alloc]).andReturn(mockController);
OCMStub([mockController initWithAuthenticationToken:OCMOCK_ANY authConfig:OCMOCK_ANY]).andReturn(mockController);
// Expect the method that needs to be called correctly
OCMExpect([mockController handleAuthResponseWithURL:url]);
// Call the method which does the work
[self.myClassInstance authStarted];
OCMVerifyAll(mockController);
Notlar
Eğer iki yöntemleri (alloc
ve init...
yöntemi) saplama her iki durumda da emin olun. Ayrıca, her iki tıklatma çağrısının, sınıf alayının (sınıfın kendisi değil) örneği'da yapıldığından emin olun.
Dokümanlar: durumunda OCMock features
Alternatifler
Bu (garip) çözeltide bölüm yararlı olabilir Sınıf yöntemleri nedeniyle nedeni ne olursa olsun Sizin planı ayrı olamayacağını eski kodu test etmek istiyorum. Ancak, kodu değiştirebilirseniz, bunu düzeltmeniz ve ExtClass
nesnesini, bu yöntemden ExtClass
oluşturulması için bir dize değil bir parametre olarak almanız gerekir. Üretim ve test kodunuz, bu basit örnekte değil, daha karmaşık bir gerçek yaşam durumunda, özellikle daha basit ve daha açık olacaktır.
Sizin yöntem dönüş türünüz BOOL'dur ve sonra bir dizge döndürüyorsunuz. Bunlardan herhangi birini düzenleyebilir ve değiştirebilir misiniz? Sanırım bir ip atmak istiyorsun, ama emin değilim. – e1985
Teşekkürler. Onu tamir ettim. – soguy