Skip to content

Commit

Permalink
Merge branch 'dmaclach-noclass_after_stopmocking'
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdoe committed May 24, 2020
2 parents eec655e + a227883 commit f7141a3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
1 change: 1 addition & 0 deletions Source/OCMock/OCClassMockObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ - (Class)mockedClass
return mockedClass;
}


#pragma mark Extending/overriding superclass behaviour

- (void)stopMocking
Expand Down
6 changes: 4 additions & 2 deletions Source/OCMock/OCMockObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ - (NSString *)description

- (void)addStub:(OCMInvocationStub *)aStub
{
[self assertInvocationsArrayIsPresent];
@synchronized(stubs)
{
[stubs addObject:aStub];
Expand All @@ -143,8 +144,9 @@ - (void)addExpectation:(OCMInvocationExpectation *)anExpectation

- (void)assertInvocationsArrayIsPresent
{
if(invocations == nil) {
[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];
if(invocations == nil)
{
[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];
}
}

Expand Down
7 changes: 7 additions & 0 deletions Source/OCMockTests/OCMockObjectClassMethodMockingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,11 @@ - (void)testArgumentsGetReleasedAfterStopMocking
XCTAssertNil(weakArgument);
}

- (void)testThrowsWhenAttemptingToStubClassMethodOnStoppedMock
{
id mock = [OCClassMockObject mockForClass:[TestClassWithClassMethods class]];
[mock stopMocking];
XCTAssertThrowsSpecificNamed([[[mock stub] andReturn:@"hello"] foo], NSException, NSInternalInconsistencyException);
}

@end
6 changes: 6 additions & 0 deletions Source/OCMockTests/OCMockObjectTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,12 @@ - (void)testBlocksAreNotConsideredNonObjectArguments
XCTAssertTrue(blockWasInvoked, @"Should not have ignored the block argument.");
}

- (void)testThrowsWhenAttemptingToStubMethodOnStoppedMock
{
[mock stopMocking];
XCTAssertThrowsSpecificNamed([[mock stub] rangeOfString:@"foo" options:0], NSException, NSInternalInconsistencyException);
}


#pragma mark returning values from stubbed methods

Expand Down
18 changes: 9 additions & 9 deletions Source/OCMockTests/OCMockObjectVerifyAfterRunTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,16 @@ - (void)testThrowsWhenVerificationIsAttemptedAfterStopMocking
[TestBaseClassForVerifyAfterRun classMethod1];
[mock stopMocking];

BOOL threw = NO;
@try {
[[mock verify] classMethod1];
} @catch (NSException *ex) {
threw = YES;
XCTAssertEqualObjects(ex.name, NSInternalInconsistencyException);
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];
XCTAssertEqualObjects(ex.reason, expectedReason);
@try
{
[[mock verify] classMethod1];
XCTFail(@"Should have thrown an exception.");
}
@catch(NSException *e)
{
XCTAssertEqualObjects([e name], NSInternalInconsistencyException);
XCTAssertTrue([[e reason] containsString:@"after stopMocking has been called"]);
}
XCTAssertTrue(threw, @"Should have thrown a NSInternalInconsistencyException when attempting to verify after stopMocking.");
}


Expand Down

0 comments on commit f7141a3

Please sign in to comment.