Skip to content

Commit

Permalink
Allow mocked messages to throw exceptions.
Browse files Browse the repository at this point in the history
There is a confirmed bug in iOS 4.0 that prevents this from working in
the simulator although it works fine with the device.

Dupe filed: http://openradar.appspot.com/8237699
  • Loading branch information
lukeredpath committed Jul 27, 2010
1 parent f86d0e9 commit c18bd3e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions Classes/LRMocky.h
Expand Up @@ -13,4 +13,5 @@
// actions
#import "LRReturnValueAction.h"
#import "LRPerformBlockAction.h"
#import "LRThrowExceptionAction.h"
#import "LRConsecutiveCallAction.h"
22 changes: 22 additions & 0 deletions Classes/LRMocky/LRThrowExceptionAction.h
@@ -0,0 +1,22 @@
//
// LRThrowExceptionAction.h
// Mocky
//
// Created by Luke Redpath on 27/07/2010.
// Copyright (c) 2010 LJR Software Limited. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "LRExpectationAction.h"

@interface LRThrowExceptionAction : NSObject <LRExpectationAction> {
NSException *exceptionToThrow;
}
- (id)initWithException:(NSException *)exception;
@end

LRThrowExceptionAction *LRA_throwException(NSException *exception);

#ifdef LRMOCKY_SHORTHAND
#define throwException LRA_throwException
#endif
38 changes: 38 additions & 0 deletions Classes/LRMocky/LRThrowExceptionAction.m
@@ -0,0 +1,38 @@
//
// LRThrowExceptionAction.m
// Mocky
//
// Created by Luke Redpath on 27/07/2010.
// Copyright (c) 2010 LJR Software Limited. All rights reserved.
//

#import "LRThrowExceptionAction.h"


@implementation LRThrowExceptionAction

- (id)initWithException:(NSException *)exception;
{
if (self = [super init]) {
exceptionToThrow = [exception retain];
}
return self;
}

- (void)dealloc
{
[exceptionToThrow release];
[super dealloc];
}

- (void)invoke:(NSInvocation *)invocation
{
[exceptionToThrow raise];
}

@end

LRThrowExceptionAction *LRA_throwException(NSException *exception)
{
return [[[LRThrowExceptionAction alloc] initWithException:exception] autorelease];
}
6 changes: 6 additions & 0 deletions Mocky.xcodeproj/project.pbxproj
Expand Up @@ -113,6 +113,7 @@
A34650D411F857680080B46B /* LRMockeryTest.m in Sources */ = {isa = PBXBuildFile; fileRef = A34650D311F857680080B46B /* LRMockeryTest.m */; };
A3C707F811FDDFE0000D2A8C /* ExpectationActionsTest.m in Sources */ = {isa = PBXBuildFile; fileRef = A3C707F711FDDFE0000D2A8C /* ExpectationActionsTest.m */; };
A3EE4EF711FE5D64002DA61D /* LRConsecutiveCallAction.m in Sources */ = {isa = PBXBuildFile; fileRef = A3EE4EF611FE5D64002DA61D /* LRConsecutiveCallAction.m */; };
A3EE4F1611FE6187002DA61D /* LRThrowExceptionAction.m in Sources */ = {isa = PBXBuildFile; fileRef = A3EE4F1511FE6187002DA61D /* LRThrowExceptionAction.m */; };
AA747D9F0F9514B9006C5449 /* Mocky_Prefix.pch in Headers */ = {isa = PBXBuildFile; fileRef = AA747D9E0F9514B9006C5449 /* Mocky_Prefix.pch */; };
AACBBE4A0F95108600F1A2B1 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AACBBE490F95108600F1A2B1 /* Foundation.framework */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -237,6 +238,8 @@
A3C707F711FDDFE0000D2A8C /* ExpectationActionsTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ExpectationActionsTest.m; sourceTree = "<group>"; };
A3EE4EF511FE5D64002DA61D /* LRConsecutiveCallAction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LRConsecutiveCallAction.h; sourceTree = "<group>"; };
A3EE4EF611FE5D64002DA61D /* LRConsecutiveCallAction.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LRConsecutiveCallAction.m; sourceTree = "<group>"; };
A3EE4F1411FE6187002DA61D /* LRThrowExceptionAction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LRThrowExceptionAction.h; sourceTree = "<group>"; };
A3EE4F1511FE6187002DA61D /* LRThrowExceptionAction.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LRThrowExceptionAction.m; sourceTree = "<group>"; };
AA747D9E0F9514B9006C5449 /* Mocky_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Mocky_Prefix.pch; sourceTree = "<group>"; };
AACBBE490F95108600F1A2B1 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
D2AAC07E0554694100DB518D /* libMocky.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libMocky.a; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -339,6 +342,8 @@
A301769711FDEEFC004828C5 /* LRReturnValueAction.m */,
A3EE4EF511FE5D64002DA61D /* LRConsecutiveCallAction.h */,
A3EE4EF611FE5D64002DA61D /* LRConsecutiveCallAction.m */,
A3EE4F1411FE6187002DA61D /* LRThrowExceptionAction.h */,
A3EE4F1511FE6187002DA61D /* LRThrowExceptionAction.m */,
);
name = Actions;
sourceTree = "<group>";
Expand Down Expand Up @@ -714,6 +719,7 @@
A34650D411F857680080B46B /* LRMockeryTest.m in Sources */,
A3C707F811FDDFE0000D2A8C /* ExpectationActionsTest.m in Sources */,
A3EE4EF711FE5D64002DA61D /* LRConsecutiveCallAction.m in Sources */,
A3EE4F1611FE6187002DA61D /* LRThrowExceptionAction.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
21 changes: 21 additions & 0 deletions Tests/ExpectationActionsTest.m
Expand Up @@ -115,4 +115,25 @@ - (void)testMocksCanReturnDifferentValuesOnConsecutiveCalls;
assertThatInt((int)[testObject returnSomeValue], equalToInt(30));
}

- (void)testMocksCanThrowAnException;
{
[context checking:^(LRExpectationBuilder *builder){
[allowing(testObject) doSomething]; [it will:throwException([NSException exceptionWithName:@"Test Exception" reason:nil userInfo:nil])];
}];

/**
* this will only pass using the iOS 4.0 Device SDK, it currently fails
* with the simulator SDK due to a runtime bug. rdar://8081169
* also see: http://openradar.appspot.com/8081169
*/

@try {
[testObject doSomething];
STFail(@"Exception expected but none was thrown");
}
@catch (NSException *exception) {
assertThat([exception name], equalTo(@"Test Exception"));
}
}

@end

0 comments on commit c18bd3e

Please sign in to comment.