Skip to content

Commit 195ac33

Browse files
committed
Merge branch 'returnValue' of git://github.com/dmaclach/ocmock into dmaclach-returnValue
2 parents 4382073 + dfd39d1 commit 195ac33

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

Source/OCMock/OCMStubRecorder.m

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,44 +51,46 @@ - (OCMInvocationStub *)stub
5151

5252
- (id)andReturn:(id)anObject
5353
{
54-
[[self stub] addInvocationAction:[[[OCMObjectReturnValueProvider alloc] initWithValue:anObject] autorelease]];
55-
return self;
54+
Class returnValueProviderClass;
55+
if (anObject == mockObject)
56+
{
57+
returnValueProviderClass = [OCMNonRetainingObjectReturnValueProvider class];
58+
} else
59+
{
60+
returnValueProviderClass = [OCMObjectReturnValueProvider class];
61+
}
62+
[[self stub] addInvocationAction:[[[returnValueProviderClass alloc] initWithValue:anObject] autorelease]];
63+
return self;
5664
}
5765

5866
- (id)andReturnValue:(NSValue *)aValue
5967
{
6068
[[self stub] addInvocationAction:[[[OCMBoxedReturnValueProvider alloc] initWithValue:aValue] autorelease]];
61-
return self;
62-
}
63-
64-
- (id)andReturnMockObject
65-
{
66-
[[self stub] addInvocationAction:[[[OCMNonRetainingObjectReturnValueProvider alloc] initWithValue:mockObject] autorelease]];
67-
return self;
69+
return self;
6870
}
6971

7072
- (id)andThrow:(NSException *)anException
7173
{
7274
[[self stub] addInvocationAction:[[[OCMExceptionReturnValueProvider alloc] initWithValue:anException] autorelease]];
73-
return self;
75+
return self;
7476
}
7577

7678
- (id)andPost:(NSNotification *)aNotification
7779
{
7880
[[self stub] addInvocationAction:[[[OCMNotificationPoster alloc] initWithNotification:aNotification] autorelease]];
79-
return self;
81+
return self;
8082
}
8183

8284
- (id)andCall:(SEL)selector onObject:(id)anObject
8385
{
8486
[[self stub] addInvocationAction:[[[OCMIndirectReturnValueProvider alloc] initWithProvider:anObject andSelector:selector] autorelease]];
85-
return self;
87+
return self;
8688
}
8789

8890
- (id)andDo:(void (^)(NSInvocation *))aBlock
8991
{
9092
[[self stub] addInvocationAction:[[[OCMBlockCaller alloc] initWithCallBlock:aBlock] autorelease]];
91-
return self;
93+
return self;
9294
}
9395

9496
- (id)andForwardToRealObject
@@ -122,7 +124,7 @@ @implementation OCMStubRecorder (Properties)
122124
{
123125
id objValue = nil;
124126
[aValue getValue:&objValue];
125-
return (objValue == mockObject) ? [self andReturnMockObject] : [self andReturn:objValue];
127+
return [self andReturn:objValue];
126128
}
127129
else
128130
{

Source/OCMockTests/OCMockObjectTests.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ - (void)setUp
214214
mock = [OCMockObject mockForClass:[NSString class]];
215215
}
216216

217-
218217
#pragma mark accepting stubbed methods / rejecting methods not stubbed
219218

220219
- (void)testAcceptsStubbedMethod
@@ -574,6 +573,18 @@ - (void)testReturnsStubbedValueForProperty
574573
}
575574

576575
- (void)testReturningMockFromMethodItStubsDoesntCreateRetainCycle
576+
{
577+
@autoreleasepool {
578+
id mockWithShortLifetime = OCMClassMock([TestClassWithClassMethod class]);
579+
[[[mockWithShortLifetime stub] andReturn:@"bar"] stringValue];
580+
[[[mockWithShortLifetime stub] andReturn:mockWithShortLifetime] shared];
581+
}
582+
id singleton = [TestClassWithClassMethod shared];
583+
584+
XCTAssertEqualObjects(@"foo", [singleton stringValue], @"Should return value from real implementation (because shared is not stubbed anymore).");
585+
}
586+
587+
- (void)testReturningMockFromMethodItStubsDoesntCreateRetainCycleUsingMacros
577588
{
578589
@autoreleasepool {
579590
id mockWithShortLifetime = OCMClassMock([TestClassWithClassMethod class]);
@@ -586,6 +597,7 @@ - (void)testReturningMockFromMethodItStubsDoesntCreateRetainCycle
586597
}
587598

588599

600+
589601
#pragma mark beyond stubbing: raising exceptions, posting notifications, etc.
590602

591603
- (void)testRaisesExceptionWhenAskedTo

0 commit comments

Comments
 (0)