Closed marcuswu0814 closed 4 years ago
I get the same error too, exc_bad_access, everything works perfect when test target is ios 8 or 9, but ios 10 simulator in xcode 8 throws an error.
when I put breakpoints I see that this line causes it
id mockSharedInstance = OCMPartialMock([AVAudioSession sharedInstance]);
Looks like an infinite loop to me.
whole function is
- (void)testPresentFromRootPermissionReportResult {
id deviceUtilMock = OCMClassMock([XMDeviceUtil class]);
OCMStub([deviceUtilMock isSimulator]).andReturn(NO);
id strategyMock = OCMClassMock([XAPermissionStrategy class]);
id viewControllerMock = OCMClassMock([UIViewController class]);
XAStrategyResult *result = [XAStrategyResult resultForStrategy:strategyMock];
result = [result requestRecordPermissionResult:YES];
NSError *error = [[NSError alloc] init];
//Setup the mock for AVAudioSession, //They have to not have permission
id mockSharedInstance = OCMPartialMock([AVAudioSession sharedInstance]);
OCMStub([mockSharedInstance recordPermission]).andReturn(AVAudioSessionRecordPermissionUndetermined);
XAPermissionStrategyService *service = [XAPermissionStrategyService serviceWithDevServer:YES];
id servicePartialMock = OCMPartialMock(service);
OCMExpect([servicePartialMock reportPermissionStrategyResult:result success:[OCMArg any] failure:[OCMArg any]]);
id settingsMock = OCMClassMock([XASettings class]);
id permissionServiceMock = OCMClassMock([XAPermissionStrategyService class]);
OCMStub([permissionServiceMock service]).andReturn(servicePartialMock);
//Expect this to be called and pass the args through
OCMExpect([strategyMock fromViewController:viewControllerMock requestRecordPermission:([OCMArg invokeBlockWithArgs:result, error, nil])]);
//the test
XARecordPermissionCompanionViewController *companion = [[XARecordPermissionCompanionViewController alloc] initWithStrategy:strategyMock settings:settingsMock];
[companion presentFromViewController:viewControllerMock];
OCMVerifyAllWithDelay(strategyMock, 1);
OCMVerifyAllWithDelay(servicePartialMock, 1);
}
This solved it but I am not sure if it is the correct way to do it?
https://blog.uship.com/shippingcode/unit-testing-around-singletons-with-ocmock/
id mockSharedInstance = [OCMockObject niceMockForClass:[AVAudioSession class]];
[[[mockSharedInstance stub] andReturn:mockSharedInstance] sharedInstance];
OCMStub([mockSharedInstance recordPermission]).andReturn(AVAudioSessionRecordPermissionDenied);
....test then
[mockSharedInstance stopMocking];
I see random crashes while using OCMock for iOS 10 simulator. Is there any plan to release a new version for iOS 10
Closing due to inactivity.
I running my unit test with OCMock, it always crash in specific test method when I run all the test method (about 20), but if I run the test method alone, it passed.
bad access crash in
OCPartialMockObject
's methodforwardInvocationForRealObject:
's[anInvocation invoke];
:I guess my mock viewController be dealloc, but why? If need much more crash information, please let me know, thanks!
BTW, the test suite passed all the time when I running test on iOS 9 simulator.