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

Closed
KaIKuxy opened this issue Apr 11, 2024 · 1 comment
Closed

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

KaIKuxy opened this issue Apr 11, 2024 · 1 comment

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.

@erikdoe
Copy link
Owner

erikdoe commented Oct 5, 2024

You're reading too much into what's printed. Yes, at some point that exception is thrown in the code you found, but that exception get's caught and unwrapped later:

@catch(NSException *e)
{
if([[e name] isEqualToString:OCMStubbedException])
{
e = [[e userInfo] objectForKey:@"exception"];
}
else
{
// add non-stubbed method to list of exceptions to be re-raised in verify
@synchronized(exceptions)
{
[exceptions addObject:e];
}
}
[e raise];
}

There's a unit test that shows the correct behaviour:

- (void)testSetsUpExceptionThrowing
{
id mock = OCMClassMock([NSString class]);
OCMStub([mock uppercaseString]).andThrow([NSException exceptionWithName:@"TestException" reason:@"Testing" userInfo:nil]);
XCTAssertThrowsSpecificNamed([mock uppercaseString], NSException, @"TestException", @"Should have thrown correct exception");
}

In your dummy, please check what exception is actually thrown.

@erikdoe erikdoe closed this as completed Oct 5, 2024
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

2 participants