diff --git a/Source/OCMock/OCMExpectationRecorder.h b/Source/OCMock/OCMExpectationRecorder.h index 405aaa43..41665695 100644 --- a/Source/OCMock/OCMExpectationRecorder.h +++ b/Source/OCMock/OCMExpectationRecorder.h @@ -18,6 +18,8 @@ @interface OCMExpectationRecorder : OCMStubRecorder +@property(retain) OCMLocation *location; + - (id)never; @end diff --git a/Source/OCMock/OCMExpectationRecorder.m b/Source/OCMock/OCMExpectationRecorder.m index 38701d15..cc82c32f 100644 --- a/Source/OCMock/OCMExpectationRecorder.m +++ b/Source/OCMock/OCMExpectationRecorder.m @@ -39,6 +39,7 @@ - (OCMInvocationExpectation *)expectation - (id)never { + [[self expectation] setLocation:[self location]]; [[self expectation] setMatchAndReject:YES]; return self; } diff --git a/Source/OCMock/OCMInvocationExpectation.h b/Source/OCMock/OCMInvocationExpectation.h index aa53f1e7..0a5d9b7d 100644 --- a/Source/OCMock/OCMInvocationExpectation.h +++ b/Source/OCMock/OCMInvocationExpectation.h @@ -16,12 +16,16 @@ #import "OCMInvocationStub.h" +@class OCMLocation; + @interface OCMInvocationExpectation : OCMInvocationStub { BOOL matchAndReject; BOOL isSatisfied; } +@property(retain) OCMLocation *location; + - (void)setMatchAndReject:(BOOL)flag; - (BOOL)isMatchAndReject; diff --git a/Source/OCMock/OCMInvocationExpectation.m b/Source/OCMock/OCMInvocationExpectation.m index a56771df..b4c382c9 100644 --- a/Source/OCMock/OCMInvocationExpectation.m +++ b/Source/OCMock/OCMInvocationExpectation.m @@ -15,6 +15,7 @@ */ #import "OCMInvocationExpectation.h" +#import "OCMFunctionsPrivate.h" #import "NSInvocation+OCMAdditions.h" @@ -44,8 +45,9 @@ - (void)handleInvocation:(NSInvocation *)anInvocation if(matchAndReject) { isSatisfied = NO; - [NSException raise:NSInternalInconsistencyException format:@"%@: explicitly disallowed method invoked: %@", - [self description], [anInvocation invocationDescription]]; + NSString *description = [NSString stringWithFormat:@"%@: explicitly disallowed method invoked: %@", + [self description], [anInvocation invocationDescription]]; + OCMReportFailure([self location], description); } else { diff --git a/Source/OCMock/OCMMacroState.h b/Source/OCMock/OCMMacroState.h index a5dda11a..18cd8b40 100644 --- a/Source/OCMock/OCMMacroState.h +++ b/Source/OCMock/OCMMacroState.h @@ -35,7 +35,7 @@ + (void)beginExpectMacro; + (OCMStubRecorder *)endExpectMacro; -+ (void)beginRejectMacro; ++ (void)beginRejectMacroAtLocation:(OCMLocation *)aLocation; + (OCMStubRecorder *)endRejectMacro; + (void)beginVerifyMacroAtLocation:(OCMLocation *)aLocation; diff --git a/Source/OCMock/OCMMacroState.m b/Source/OCMock/OCMMacroState.m index 2842ab5d..43bd6730 100644 --- a/Source/OCMock/OCMMacroState.m +++ b/Source/OCMock/OCMMacroState.m @@ -66,9 +66,11 @@ + (OCMStubRecorder *)endExpectMacro } -+ (void)beginRejectMacro ++ (void)beginRejectMacroAtLocation:(OCMLocation *)aLocation { OCMExpectationRecorder *recorder = [[[OCMExpectationRecorder alloc] init] autorelease]; + [recorder setLocation:aLocation]; + [recorder never]; OCMMacroState *macroState = [[OCMMacroState alloc] initWithRecorder:recorder]; [NSThread currentThread].threadDictionary[OCMGlobalStateKey] = macroState; [macroState release]; diff --git a/Source/OCMock/OCMock.h b/Source/OCMock/OCMock.h index 548e2147..4008fd0a 100644 --- a/Source/OCMock/OCMock.h +++ b/Source/OCMock/OCMock.h @@ -82,7 +82,7 @@ #define OCMReject(invocation) \ ({ \ _OCMSilenceWarnings( \ - [OCMMacroState beginRejectMacro]; \ + [OCMMacroState beginRejectMacroAtLocation:OCMMakeLocation(self, __FILE__, __LINE__)]; \ OCMStubRecorder *recorder = nil; \ @try{ \ invocation; \ diff --git a/Source/OCMockTests/OCMockObjectMacroTests.m b/Source/OCMockTests/OCMockObjectMacroTests.m index c4ba9f87..d434a497 100644 --- a/Source/OCMockTests/OCMockObjectMacroTests.m +++ b/Source/OCMockTests/OCMockObjectMacroTests.m @@ -284,10 +284,17 @@ - (void)testSetsUpReject { id mock = OCMClassMock([TestClassForMacroTesting class]); - OCMReject([mock stringValue]); + OCMReject([mock stringValue]); const char *expectedFile = __FILE__; int expectedLine = __LINE__; XCTAssertNoThrow([mock verify], @"Should have accepted invocation rejected method not being invoked"); - XCTAssertThrows([mock stringValue], @"Should have complained during rejected method being invoked"); + + shouldCaptureFailure = YES; + [mock stringValue]; + shouldCaptureFailure = NO; + XCTAssertNotNil(reportedDescription, @"Should have recorded a failure with description."); + XCTAssertEqualObjects([NSString stringWithUTF8String:expectedFile], reportedFile, @"Should have reported correct file."); + XCTAssertEqual(expectedLine, (int)reportedLine, @"Should have reported correct line"); + XCTAssertThrows([mock verify], @"Should have complained about rejected method being invoked"); }