Skip to content
This repository has been archived by the owner on Mar 18, 2020. It is now read-only.

Commit

Permalink
Now re-throwing fail fast exceptions on verify.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.mulle-kybernetik.com/OCMock/trunk@15 12620d81-68e2-0310-ae40-e3f550779089
  • Loading branch information
erik committed Jun 4, 2007
1 parent a739c65 commit 9147762
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Source/OCMockObject.h
@@ -1,6 +1,6 @@
//---------------------------------------------------------------------------------------
// $Id$
// Copyright (c) 2004,2005 by Mulle Kybernetik. See License file for details.
// Copyright (c) 2004-2007 by Mulle Kybernetik. See License file for details.
//---------------------------------------------------------------------------------------

#import <Foundation/Foundation.h>
Expand All @@ -10,6 +10,7 @@
BOOL isNice;
NSMutableArray *recorders;
NSMutableSet *expectations;
NSMutableArray *exceptions;
}

+ (id)mockForClass:(Class)aClass;
Expand Down
17 changes: 13 additions & 4 deletions Source/OCMockObject.m
@@ -1,6 +1,6 @@
//---------------------------------------------------------------------------------------
// $Id$
// Copyright (c) 2004,2005 by Mulle Kybernetik. See License file for details.
// Copyright (c) 2004-2007 by Mulle Kybernetik. See License file for details.
//---------------------------------------------------------------------------------------

#import "OCMockObject.h"
Expand Down Expand Up @@ -58,13 +58,15 @@ - (id)init
{
recorders = [[NSMutableArray alloc] init];
expectations = [[NSMutableSet alloc] init];
exceptions = [[NSMutableArray alloc] init];
return self;
}

- (void)dealloc
{
[recorders release];
[expectations release];
[exceptions release];
[super dealloc];
}

Expand Down Expand Up @@ -105,11 +107,15 @@ - (void)verify
[NSException raise:NSInternalInconsistencyException format:@"%@: expected method was not invoked: %@",
[self description], [[expectations anyObject] description]];
}
else if([expectations count] > 0)
if([expectations count] > 0)
{
[NSException raise:NSInternalInconsistencyException format:@"%@ : %d expected methods were not invoked: %@",
[self description], [expectations count], [self _recorderDescriptions:YES]];
}
if([exceptions count] > 0)
{
[[exceptions objectAtIndex:0] raise];
}
}


Expand Down Expand Up @@ -140,8 +146,11 @@ - (void)forwardInvocation:(NSInvocation *)anInvocation
}
else if(isNice == NO)
{
[NSException raise:NSInternalInconsistencyException format:@"%@: unexpected method invoked: %@ %@",
[self description], [anInvocation invocationDescription], [self _recorderDescriptions:NO]];
NSException *exception = [NSException exceptionWithName:NSInternalInconsistencyException reason:
[NSString stringWithFormat:@"%@: unexpected method invoked: %@ %@", [self description],
[anInvocation invocationDescription], [self _recorderDescriptions:NO]] userInfo:nil];
[exceptions addObject:exception];
[exception raise];
}

}
Expand Down
17 changes: 15 additions & 2 deletions Source/OCMockObjectTests.m
@@ -1,6 +1,6 @@
//---------------------------------------------------------------------------------------
// $Id$
// Copyright (c) 2004 by Mulle Kybernetik. See License file for details.
// Copyright (c) 2004-2007 by Mulle Kybernetik. See License file for details.
//---------------------------------------------------------------------------------------

#import "OCMock.h"
Expand Down Expand Up @@ -292,13 +292,26 @@ - (void)testReturnDefaultValueWhenUnknownMethodIsCalledOnProtocolMock
[mock verify];
}

- (void)testEaisesAnExceptionWenAnExpectedMethodIsNotCalledOnNiceProtocolMock
- (void)testRaisesAnExceptionWenAnExpectedMethodIsNotCalledOnNiceProtocolMock
{
mock = [OCMockObject niceMockForProtocol:@protocol(TestProtocol)];
[[mock expect] primitiveValue];
STAssertThrows([mock verify], @"Should have raised an exception because method was not called.");
}

- (void)testReRaisesFailFastExceptionsOnVerify
{
@try
{
[mock lowercaseString];
}
@catch(NSException *exception)
{
// expected
}
STAssertThrows([mock verify], @"Should have reraised the exception.");
}


/*
- (void)testCanMockInformalProtocol
Expand Down

0 comments on commit 9147762

Please sign in to comment.