Permalink
Browse files

Merge branch 'master' of github.com:allending/Kiwi into method-stubbing

  • Loading branch information...
2 parents 1e618bf + 2d2a35c commit b7a5cb448b36b55f2b35c99dfaec570947bf9a2b @stepanhruda stepanhruda committed Feb 10, 2013
Showing with 26 additions and 0 deletions.
  1. +1 −0 Classes/NSObject+KiwiStubAdditions.h
  2. +17 −0 Classes/NSObject+KiwiStubAdditions.m
  3. +8 −0 Tests/KWRealObjectStubTest.m
@@ -16,6 +16,7 @@
#pragma mark Stubbing Methods
- (void)stub:(SEL)aSelector;
+- (void)stub:(SEL)aSelector withBlock:(id (^)(NSArray *params))block;
- (void)stub:(SEL)aSelector withArguments:(id)firstArgument, ...;
- (void)stub:(SEL)aSelector andReturn:(id)aValue;
- (void)stub:(SEL)aSelector andReturn:(id)aValue withArguments:(id)firstArgument, ...;
@@ -51,6 +51,11 @@ - (void)stub:(SEL)aSelector {
[self stubMessagePattern:messagePattern andReturn:nil];
}
+- (void)stub:(SEL)aSelector withBlock:(id (^)(NSArray *))block {
+ KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
+ [self stubMessagePattern:messagePattern withBlock:block];
+}
+
- (void)stub:(SEL)aSelector withArguments:(id)firstArgument, ... {
va_list argumentList;
va_start(argumentList, firstArgument);
@@ -101,6 +106,18 @@ - (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern andReturn:(id)aVa
KWAssociateObjectStub(self, stub);
}
+- (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern withBlock:(id (^)(NSArray *params))block {
+ if ([self methodSignatureForSelector:aMessagePattern.selector] == nil) {
+ [NSException raise:@"KWStubException" format:@"cannot stub -%@ because no such method exists",
+ NSStringFromSelector(aMessagePattern.selector)];
+ }
+
+ Class interceptClass = KWSetupObjectInterceptSupport(self);
+ KWSetupMethodInterceptSupport(interceptClass, aMessagePattern.selector);
+ KWStub *stub = [KWStub stubWithMessagePattern:aMessagePattern block:block];
+ KWAssociateObjectStub(self, stub);
+}
+
- (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern andReturn:(id)aValue times:(id)times afterThatReturn:(id)aSecondValue {
if ([self methodSignatureForSelector:aMessagePattern.selector] == nil) {
[NSException raise:@"KWStubException" format:@"cannot stub -%@ because no such method exists",
@@ -179,6 +179,14 @@ - (void)testCallingCaptureArgumentOnRealObjectThrowsException {
STAssertThrows([cruiser captureArgument:@selector(foo) atIndex:0], @"expected to throw exception");
}
+- (void)testItShouldStubWithBlock {
+ Cruiser *cruiser = [Cruiser cruiser];
+ [cruiser stub:@selector(classification) withBlock:^id(NSArray *params) {
+ return @"Enterprise";
+ }];
+ STAssertEquals([cruiser classification], @"Enterprise", @"expected method to be stubbed with block");
+}
+
@end
#endif // #if KW_TESTS_ENABLED

0 comments on commit b7a5cb4

Please sign in to comment.