Skip to content

Commit

Permalink
Warning that stubbing init method doesn't work.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdoe committed Jun 30, 2016
1 parent 3808637 commit 9eca5f7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Source/Changes.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Listing of notable changes by release. More detail is usually found in the Git
commit messages and/or the pull requests.

(unreleased)

* Now throwing an exception when an attempt is made to stub the init method.
* Fixed crash when trying to mock NSArray.


OCMock 3.3 (2016-04-12)

* Made the use of mock objects thread safe. You still have to setup the mocks
Expand Down
3 changes: 3 additions & 0 deletions Source/OCMock/OCMStubRecorder.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ @implementation OCMStubRecorder

- (id)init
{
if(invocationMatcher != nil)
[NSException raise:NSInternalInconsistencyException format:@"** Method init invoked twice on stub recorder. Are you trying to mock the init method? This is currently not supported."];

This comment has been minimized.

Copy link
@valeriomazzeo

valeriomazzeo Jul 9, 2016

I am not sure what's happening here, but one test that before this change was fine, now is failing.
OCMStub([OCMPartialMock([MyClass sharedInstance]) myProperty]).andReturn(@(1234));

I don't think I am trying to do something crazy in here, but it doesn't work anymore.

This comment has been minimized.

Copy link
@erikdoe

erikdoe Jul 9, 2016

Author Owner

You're running into the limitations caused by the use of macros. Can you move the creation of the mock, i.e. OCMPartialMock(), out of the OCMStub() macro? Of course you need a local variable.

This comment has been minimized.

Copy link
@valeriomazzeo

valeriomazzeo Jul 9, 2016

I tried that too, I get the same result :(

This comment has been minimized.

Copy link
@erikdoe

erikdoe Jul 10, 2016

Author Owner

Okay, this is getting too complicated to discuss here. Can you please open an issue with an example that shows the problem? Thanks.

This comment has been minimized.

Copy link
@valeriomazzeo

valeriomazzeo Jul 10, 2016

sure. will do

self = [super init];
invocationMatcher = [[OCMInvocationStub alloc] init];
return self;
Expand Down
8 changes: 8 additions & 0 deletions Source/OCMock/OCMockObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ + (id)observerMock

- (instancetype)init
{
// check if we are called from inside a macro
OCMRecorder *recorder = [[OCMMacroState globalState] recorder];
if(recorder != nil)
{
[recorder setMockObject:self];
return (id)[recorder init];
}

// no [super init], we're inheriting from NSProxy
expectationOrderMatters = NO;
stubs = [[NSMutableArray alloc] init];
Expand Down
12 changes: 12 additions & 0 deletions Source/OCMockTests/OCMockObjectRuntimeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ - (void)testComplainsWhenUnimplementedMethodIsCalled
XCTAssertThrowsSpecificNamed([mock performSelector:@selector(sortedArrayHint)], NSException, NSInvalidArgumentException);
}

- (void)testComplainsWhenAttemptIsMadeToStubInitMethod
{
id mock = [OCMockObject mockForClass:[NSString class]];
XCTAssertThrows([[[mock stub] init] andReturn:nil]);
}

- (void)testComplainsWhenAttemptIsMadeToStubInitMethodViaMacro
{
id mock = [OCMockObject mockForClass:[NSString class]];
XCTAssertThrows(OCMStub([mock init]));
}


- (void)testMockShouldNotRaiseWhenDescribing
{
Expand Down

0 comments on commit 9eca5f7

Please sign in to comment.