Created
November 7, 2012 06:33
-
-
Save sgleadow/4029858 to your computer and use it in GitHub Desktop.
Example of stubbing and mocking class methods using Kiwi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Kiwi.h" | |
@interface MyFactory : NSObject | |
+ (id)makeThingsCalled:(NSString *)name; | |
@end | |
@implementation MyFactory | |
+ (id)makeThingsCalled:(NSString *)name; | |
{ | |
// we're going to stub this out anyway | |
return @"some object to return"; | |
} | |
@end | |
@interface MyObject : NSObject | |
- (id)doAllTheThings; | |
@end | |
@implementation MyObject | |
- (id)doAllTheThings; | |
{ | |
return [MyFactory makeThingsCalled:@"scott"]; | |
} | |
@end | |
SPEC_BEGIN(MyObjectSpec) | |
describe(@"test mocks on a class method", ^{ | |
it(@"should behave as usual without stubs and mocks", ^{ | |
MyObject *obj = [[MyObject alloc] init]; | |
[[[obj doAllTheThings] should] equal:@"some object to return"]; | |
}); | |
it(@"should stub the class method", ^{ | |
MyObject *obj = [[MyObject alloc] init]; | |
[[MyFactory stubAndReturn:@"another object"] makeThingsCalled:any()]; | |
[[[obj doAllTheThings] should] equal:@"another object"]; | |
}); | |
it(@"should expect the factory method and replace the implementation", ^{ | |
MyObject *obj = [[MyObject alloc] init]; | |
[[[MyFactory should] receiveAndReturn:@"yet another object"] makeThingsCalled:@"scott"]; | |
[[[obj doAllTheThings] should] equal:@"yet another object"]; | |
}); | |
}); | |
SPEC_END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment