Skip to content
Browse files

Merge pull request #178 from eraserhd/support_kvc_stubbing

Support stubbing KVC methods
  • Loading branch information...
2 parents a92ac82 + 7ef3232 commit 879a1b931fe1e612e0060f8a38fcb23dba880425 @allending committed Dec 13, 2012
Showing with 91 additions and 0 deletions.
  1. +41 −0 Kiwi/KWMock.m
  2. +50 −0 Tests/KWMockTest.m
View
41 Kiwi/KWMock.m
@@ -590,4 +590,45 @@ - (id)mutableCopy {
}
}
+#pragma mark -
+#pragma mark Key-Value Coding Support
+
+static id valueForKeyImplementation(id self, SEL _cmd, id key) {
+ KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:_cmd];
+ [self expectMessagePattern:messagePattern];
+ NSInvocation *invocation = [NSInvocation invocationWithTarget:self selector:_cmd messageArguments:&key];
+
+ if ([self processReceivedInvocation:invocation]) {
+ id result = nil;
+ [invocation getReturnValue:&result];
+ return result;
+ } else {
+ return nil;
+ }
+}
+
+- (id)valueForKey:(NSString *)key {
+ return valueForKeyImplementation(self, _cmd, key);
+}
+
+- (id)valueForKeyPath:(NSString *)keyPath {
+ return valueForKeyImplementation(self, _cmd, keyPath);
+}
+
+static void setValueForKeyImplementation(id self, SEL _cmd, id a, id b) {
+ KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:_cmd];
+ [self expectMessagePattern:messagePattern];
+ NSInvocation *invocation = [NSInvocation invocationWithTarget:self selector:_cmd messageArguments:&a, &b];
+
+ [self processReceivedInvocation:invocation];
+}
+
+- (void)setValue:(id)value forKey:(NSString *)key {
+ setValueForKeyImplementation(self, _cmd, value, key);
+}
+
+- (void)setValue:(id)value forKeyPath:(NSString *)keyPath {
+ setValueForKeyImplementation(self, _cmd, value, keyPath);
+}
+
@end
View
50 Tests/KWMockTest.m
@@ -297,6 +297,56 @@ - (void)testItShouldStubInitAndReturnSelfByDefaultForNullMocks {
STAssertEquals(cruiser, mock, @"expected init to be stubbed");
}
+- (void)testItShouldNotRaiseWhenReceivingKVCMessagesAsANullMock {
+ id mock = [Cruiser nullMock];
+ STAssertNoThrow([mock valueForKey:@"foo"], @"expected valueForKey: not to raise");
+ STAssertNoThrow([mock setValue:@"bar" forKey:@"foo"], @"expected setValue:forKey not to raise");
+ STAssertNoThrow([mock valueForKeyPath:@"foo.bar"], @"expected valueForKeyPath: not to raise");
+ STAssertNoThrow([mock setValue:@"baz" forKeyPath:@"foo.bar"], @"expected setValue:forKeyPath: not to raise");
+}
+
+- (void)testItShouldAllowStubbingValueForKey {
+ id mock = [Cruiser mock];
+ id otherMock = [Cruiser mock];
+ [mock stub:@selector(valueForKey:) andReturn:otherMock withArguments:@"foo"];
+ id value = [mock valueForKey:@"foo"];
+ STAssertEquals(value, otherMock, @"expected valueForKey: to be stubbed");
+}
+
+- (void)testItShouldAllowStubbingValueForKeyPath {
+ id mock = [Cruiser mock];
+ id otherMock = [Cruiser mock];
+ [mock stub:@selector(valueForKeyPath:) andReturn:otherMock withArguments:@"foo.bar"];
+ id value = [mock valueForKeyPath:@"foo.bar"];
+ STAssertEquals(value, otherMock, @"expected valueForKeyPath: to be stubbed");
+}
+
+- (void)testItShouldAllowStubbingSetValueForKey {
+ id mock = [Cruiser mock];
+ __block BOOL called = NO;
+ [mock stub:@selector(setValue:forKey:) withBlock:^id(NSArray *params) {
+ STAssertEquals([params objectAtIndex:0], @"baz", @"expected arg 1 of setValue:forKey: to be 'baz'");
+ STAssertEquals([params objectAtIndex:1], @"foo", @"expected arg 2 of setValue:forKey: to be 'foo'");
+ called = YES;
+ return nil;
+ }];
+ [mock setValue:@"baz" forKey:@"foo"];
+ STAssertTrue(called, @"expected setValue:forKey: to be stubbed");
+}
+
+- (void)testItShouldAllowStubbingSetValueForKeyPath {
+ id mock = [Cruiser mock];
+ __block BOOL called = NO;
+ [mock stub:@selector(setValue:forKeyPath:) withBlock:^id(NSArray *params) {
+ STAssertEquals([params objectAtIndex:0], @"baz", @"expected arg 1 of setValue:forKeyPath: to be 'baz'");
+ STAssertEquals([params objectAtIndex:1], @"foo.bar", @"expected arg 2 of setValue:forKey: to be 'foo.bar'");
+ called = YES;
+ return nil;
+ }];
+ [mock setValue:@"baz" forKeyPath:@"foo.bar"];
+ STAssertTrue(called, @"expected setValue:forKeyPath: to be stubbed");
+}
+
@end
#endif // #if KW_TESTS_ENABLED

0 comments on commit 879a1b9

Please sign in to comment.
Something went wrong with that request. Please try again.