Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.andThrow(anException) will not throw the provided anException #538

Open
KaIKuxy opened this issue Apr 11, 2024 · 0 comments
Open

.andThrow(anException) will not throw the provided anException #538

KaIKuxy opened this issue Apr 11, 2024 · 0 comments

Comments

@KaIKuxy
Copy link

KaIKuxy commented Apr 11, 2024

Based on the documentation:

OCMStub([mock someMethod]).andThrow(anException);

When someMethod is invoked the stub will throw anException.

However it seems like a special exception is thrown instead of the provided one, the provided one is wrapped inside the userInfo

@implementation OCMExceptionReturnValueProvider
NSString *OCMStubbedException = @"OCMStubbedException";
- (void)handleInvocation:(NSInvocation *)anInvocation
{
[[NSException exceptionWithName:OCMStubbedException reason:@"Exception stubbed in test." userInfo:@{ @"exception" : returnValue }] raise];
}
@end


Repro

@interface Dummy : NSObject
- (void)method;
- (void)throwException;
@end

@implementation Dummy
- (void)method {
    @try {
        [self throwException];
    } @catch (NSException *exception) {
        NSLog(@"exception %@ %@", exception.name, exception.reason);
    }
}
- (void)throwException { }
@end

@interface DummyTests : XCTestCase
@end
@implementation DummyTests
- (void)testDummy {
    id dummyPartialMock = OCMPartialMock([Dummy new]);
    OCMStub([dummyPartialMock throwException]).andThrow([NSException exceptionWithName:@"name" reason:@"reason" userInfo:nil]);
    [dummyPartialMock method];
}
@end

The printed message will be exception OCMStubbedException Exception stubbed in test instead of exception name reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant