Skip to content

Instantly share code, notes, and snippets.

@sgleadow
Created November 7, 2012 06:33
Show Gist options
  • Save sgleadow/4029858 to your computer and use it in GitHub Desktop.
Save sgleadow/4029858 to your computer and use it in GitHub Desktop.
Example of stubbing and mocking class methods using Kiwi
#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