我遇到了问题。我有OCUnit测试。我可以在iOS 7模拟器中成功运行它们,但我在iOS 6模拟器上获得了EXC_BAD_ACCESS。
这是Xcode的截图:
我正在使用OCMockito
和OCHamcrest
。
你知道原因是什么吗?我试图调查并找出原因。
更新
这里是测试用例的代码(至少来自日志):
static NSString *const EMAIL = @"foo@example.com";
static NSString *const PASSWORD = @"secret";
@interface MDLoginViewControllerTest : SenTestCase
@end
@implementation MDLoginViewControllerTest {
MDLoginViewController *controller;
MDLoginView *loginView;
UIWindow *window;
MDAlertManager *mockedAlertManager;
MDNetwork *mockedNetwork;
MDStorage *mockedStorage;
}
- (void)setUp
{
[super setUp];
mockedAlertManager = mock([MDAlertManager class]);
mockedNetwork = mock([MDNetwork class]);
mockedStorage = mock([MDStorage class]);
controller = [[MDLoginViewController alloc] initWithAlert:mockedAlertManager andNetwork:mockedNetwork andStorage:mockedStorage];
window = [[UIWindow alloc] init];
[window addSubview:controller.view];
}
# pragma mark - Test Helpers
- (void)login
{
[controller requestDiscoveryWithEmail:EMAIL andPassword:PASSWORD];
}
- (void)mockLoginView
{
loginView = mock([MDLoginView class]);
controller.loginView = loginView;
}
- (void (^)(NSError *))runControllerForgotPasswordForEmailAndReturnBlock
{
[controller recoverAccountPasswordForEmail:EMAIL];
MKTArgumentCaptor *captor = [MKTArgumentCaptor new];
[verify(mockedNetwork) forgotPasswordForEmail:EMAIL andCallback:[captor capture]];
void (^callback)(NSError *) = [captor value];
return callback;
}
# pragma mark - Tests itself
- (void)testAlertThatInputIsEmptyWhenLoginIsNil
{
[controller requestDiscoveryWithEmail:nil andPassword:@"pass"];
[verify(mockedAlertManager) showAlertMessage:EMPTY_DATA_MESSAGE withTitle:WRONG_DATA_TITLE];
[verifyCount(mockedNetwork, never()) discoveryWithEmail:anything() andPassword:anything() andCallback:anything()];
}
- (void)testAlertThatInputIsEmptyWhenLoginIsWhitespacesOnly
{
[controller requestDiscoveryWithEmail:@" " andPassword:@"pass"];
[verify(mockedAlertManager) showAlertMessage:EMPTY_DATA_MESSAGE withTitle:WRONG_DATA_TITLE];
[verifyCount(mockedNetwork, never()) discoveryWithEmail:anything() andPassword:anything() andCallback:anything()];
}
- (void)testAlertThatInputIsEmptyWhenPasswordIsNil
{
[controller requestDiscoveryWithEmail:@"test@t.com" andPassword:nil];
[verify(mockedAlertManager) showAlertMessage:EMPTY_DATA_MESSAGE withTitle:WRONG_DATA_TITLE];
[verifyCount(mockedNetwork, never()) discoveryWithEmail:anything() andPassword:anything() andCallback:anything()];
}
- (void)testAlertThatInputIsEmptyWhenPasswordIsWhitespacesOnly
{
[controller requestDiscoveryWithEmail:@"test@ts.com" andPassword:@" "];
[verify(mockedAlertManager) showAlertMessage:EMPTY_DATA_MESSAGE withTitle:WRONG_DATA_TITLE];
[verifyCount(mockedNetwork, never()) discoveryWithEmail:anything() andPassword:anything() andCallback:anything()];
}
- (void)testAlertThatWrongEmailWhenEmailIsInvalid
{
[controller requestDiscoveryWithEmail:@"test" andPassword:@"passs"];
[verify(mockedAlertManager) showAlertMessage:ENTER_VALID_EMAIL_MESSAGE withTitle:WRONG_DATA_TITLE];
[verifyCount(mockedNetwork, never()) discoveryWithEmail:anything() andPassword:anything() andCallback:anything()];
}
- (void)testCallNetworkIfInputIsValid
{
[self login];
[verify(mockedNetwork) discoveryWithEmail:EMAIL andPassword:PASSWORD andCallback:anything()];
}
}
来自Xcode的更多图片:
更新:
看起来问题是在从mock调用方法的方法中,其中一个参数是在self
上有强引用的块。
以下是此方法的代码:
- (void)requestDiscoveryWithEmail:(NSString *)email andPassword:(NSString *)password
{
...
// store email / password on view
_email = [email copy];
_password = [password copy];
[self.loginView showSpinner];
[self.network discoveryWithEmail:email
andPassword:password
andCallback:^(NSDictionary *response, NSError *error) {
// Hide the spinner
[self.loginView hideSpinner];
if (error) {
[self showError:error];
}
else {
if (![self isResponseComplete:response]) {
_response = [response copy];
[self.alertManager showAlertMessage:NSLocalizedString(@"We were unable to reach some services because maintenance or some issues", @"Unreachable services message")
withTitle:NSLocalizedString(@"Issues happened", @" Issues happened title")
andDelegate:self];
}
else {
[self processDiscoveryResponse:response];
}
}
}];
}