From 4dcbbc35bbc6210af736cd0cde065bd2445cad17 Mon Sep 17 00:00:00 2001 From: Jason Felice Date: Mon, 10 Dec 2012 21:12:40 -0500 Subject: [PATCH] valueForKey: can be stubbed --- Kiwi/KWMock.m | 12 +++++++++++- Tests/KWMockTest.m | 8 ++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Kiwi/KWMock.m b/Kiwi/KWMock.m index f720c063..6f999aba 100644 --- a/Kiwi/KWMock.m +++ b/Kiwi/KWMock.m @@ -594,7 +594,17 @@ - (id)mutableCopy { #pragma mark Key-Value Coding Support - (id)valueForKey:(NSString *)key { - return nil; + 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; + } } - (void)setValue:(id)value forKey:(NSString *)keyPath { diff --git a/Tests/KWMockTest.m b/Tests/KWMockTest.m index f075f8a9..00d4b7e3 100644 --- a/Tests/KWMockTest.m +++ b/Tests/KWMockTest.m @@ -305,6 +305,14 @@ - (void)testItShouldNotRaiseWhenReceivingKVCMessagesAsANullMock { 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"); +} + @end #endif // #if KW_TESTS_ENABLED