Skip to content

Commit f7141a3

Browse files
committed
Merge branch 'dmaclach-noclass_after_stopmocking'
2 parents eec655e + a227883 commit f7141a3

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

Source/OCMock/OCClassMockObject.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ - (Class)mockedClass
5050
return mockedClass;
5151
}
5252

53+
5354
#pragma mark Extending/overriding superclass behaviour
5455

5556
- (void)stopMocking

Source/OCMock/OCMockObject.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ - (NSString *)description
127127

128128
- (void)addStub:(OCMInvocationStub *)aStub
129129
{
130+
[self assertInvocationsArrayIsPresent];
130131
@synchronized(stubs)
131132
{
132133
[stubs addObject:aStub];
@@ -143,8 +144,9 @@ - (void)addExpectation:(OCMInvocationExpectation *)anExpectation
143144

144145
- (void)assertInvocationsArrayIsPresent
145146
{
146-
if(invocations == nil) {
147-
[NSException raise:NSInternalInconsistencyException format:@"** Cannot handle or verify invocations on %@ at %p. This error usually occurs when a mock object is used after stopMocking has been called on it. In most cases it is not necessary to call stopMocking. If you know you have to, please make sure that the mock object is not used afterwards.", [self description], self];
147+
if(invocations == nil)
148+
{
149+
[NSException raise:NSInternalInconsistencyException format:@"** Cannot use mock object %@ at %p. This error usually occurs when a mock object is used after stopMocking has been called on it. In most cases it is not necessary to call stopMocking. If you know you have to, please make sure that the mock object is not used afterwards.", [self description], (void *)self];
148150
}
149151
}
150152

Source/OCMockTests/OCMockObjectClassMethodMockingTests.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,11 @@ - (void)testArgumentsGetReleasedAfterStopMocking
368368
XCTAssertNil(weakArgument);
369369
}
370370

371+
- (void)testThrowsWhenAttemptingToStubClassMethodOnStoppedMock
372+
{
373+
id mock = [OCClassMockObject mockForClass:[TestClassWithClassMethods class]];
374+
[mock stopMocking];
375+
XCTAssertThrowsSpecificNamed([[[mock stub] andReturn:@"hello"] foo], NSException, NSInternalInconsistencyException);
376+
}
377+
371378
@end

Source/OCMockTests/OCMockObjectTests.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,12 @@ - (void)testBlocksAreNotConsideredNonObjectArguments
473473
XCTAssertTrue(blockWasInvoked, @"Should not have ignored the block argument.");
474474
}
475475

476+
- (void)testThrowsWhenAttemptingToStubMethodOnStoppedMock
477+
{
478+
[mock stopMocking];
479+
XCTAssertThrowsSpecificNamed([[mock stub] rangeOfString:@"foo" options:0], NSException, NSInternalInconsistencyException);
480+
}
481+
476482

477483
#pragma mark returning values from stubbed methods
478484

Source/OCMockTests/OCMockObjectVerifyAfterRunTests.m

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,16 @@ - (void)testThrowsWhenVerificationIsAttemptedAfterStopMocking
124124
[TestBaseClassForVerifyAfterRun classMethod1];
125125
[mock stopMocking];
126126

127-
BOOL threw = NO;
128-
@try {
129-
[[mock verify] classMethod1];
130-
} @catch (NSException *ex) {
131-
threw = YES;
132-
XCTAssertEqualObjects(ex.name, NSInternalInconsistencyException);
133-
NSString *expectedReason = [NSString stringWithFormat:@"** Cannot handle or verify invocations on %@ at %p. This error usually occurs when a mock object is used after stopMocking has been called on it. In most cases it is not necessary to call stopMocking. If you know you have to, please make sure that the mock object is not used afterwards.", [mock description], mock];
134-
XCTAssertEqualObjects(ex.reason, expectedReason);
127+
@try
128+
{
129+
[[mock verify] classMethod1];
130+
XCTFail(@"Should have thrown an exception.");
131+
}
132+
@catch(NSException *e)
133+
{
134+
XCTAssertEqualObjects([e name], NSInternalInconsistencyException);
135+
XCTAssertTrue([[e reason] containsString:@"after stopMocking has been called"]);
135136
}
136-
XCTAssertTrue(threw, @"Should have thrown a NSInternalInconsistencyException when attempting to verify after stopMocking.");
137137
}
138138

139139

0 commit comments

Comments
 (0)