From 9eca5f781bd2cd362a5ea39ec98c038f9ce674c2 Mon Sep 17 00:00:00 2001 From: Erik Doernenburg Date: Thu, 30 Jun 2016 17:20:07 +0200 Subject: [PATCH] Warning that stubbing init method doesn't work. --- Source/Changes.txt | 6 ++++++ Source/OCMock/OCMStubRecorder.m | 3 +++ Source/OCMock/OCMockObject.m | 8 ++++++++ Source/OCMockTests/OCMockObjectRuntimeTests.m | 12 ++++++++++++ 4 files changed, 29 insertions(+) diff --git a/Source/Changes.txt b/Source/Changes.txt index 57276b29..d2b69deb 100644 --- a/Source/Changes.txt +++ b/Source/Changes.txt @@ -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 diff --git a/Source/OCMock/OCMStubRecorder.m b/Source/OCMock/OCMStubRecorder.m index 255d6a01..537f6e3b 100644 --- a/Source/OCMock/OCMStubRecorder.m +++ b/Source/OCMock/OCMStubRecorder.m @@ -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."]; + self = [super init]; invocationMatcher = [[OCMInvocationStub alloc] init]; return self; diff --git a/Source/OCMock/OCMockObject.m b/Source/OCMock/OCMockObject.m index b0f0d2e3..0f79f119 100644 --- a/Source/OCMock/OCMockObject.m +++ b/Source/OCMock/OCMockObject.m @@ -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]; diff --git a/Source/OCMockTests/OCMockObjectRuntimeTests.m b/Source/OCMockTests/OCMockObjectRuntimeTests.m index abea650a..e60a2ccf 100644 --- a/Source/OCMockTests/OCMockObjectRuntimeTests.m +++ b/Source/OCMockTests/OCMockObjectRuntimeTests.m @@ -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 {