【发布时间】:2014-10-28 12:36:07
【问题描述】:
我正在使用 Specta 创建一些测试,但我似乎无法通过这个基本测试。应用程序本身运行良好,但此测试无法通过。
视图控制器
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self showLogin];
}
- (void)showLogin
{
[self presentViewController:[ETLoginVC new] animated:NO completion:nil];
NSLog(@"PresentedVC: %@", [self.presentedViewController class]);
}
此日志:PresentedVC: ETLoginVC
规格
#import "Specs.h"
#import "ETLoadingVC.h"
#import "ETLoginVC.h"
SpecBegin(ETLoadingVCSpec)
describe(@"ETLoadingVC", ^{
__block ETLoadingVC *loadingVC;
beforeEach(^{
loadingVC = [[ETLoadingVC alloc] initWithUserDefaults:nil];
});
afterEach(^{
loadingVC = nil;
});
describe(@"no current user present", ^{
it(@"should have a login view controller as the presented view controller", ^{
expect(loadingVC.presentedViewController).to.beKindOf([ETLoginVC class]);
});
});
});
SpecEnd
这失败了:the actual value is nil/null
我试过打电话:
[loadingVC view]
我什至发起了一个UIWindow 和一个appDelegate,但我就是无法让它工作。
我的视图控制器都是用代码编写的。没有故事板或笔尖。
更新
现在我已经添加了一个NSString 属性,该属性会使用即将呈现的类名进行更新。然后我在我的测试中检查这个字符串。不过,为了让它工作,我不得不将我的 beforeEach 块更改为以下内容:
beforeEach(^{
loadingVC = [[ETLoadingVC alloc] initWithUserDefaults:nil];
[loadingVC viewDidAppear:NO];
});
虽然测试通过,但我收到以下消息:
Warning: Attempt to present <ETLoginVC: 0x7fcf34961940> on <ETLoadingVC: 0x7fcf34961280> whose view is not in the window hierarchy!
我知道这是因为我在当前视图完成出现之前调用了viewDidAppear。我不知道如何以更好的方式对此进行测试。
我也不明白为什么 loadingVC.presentedViewController 仍然等于 nil,即使更新了 beforeEach 块。
更新 2
将beforeEach 更改为下面的内容消除了警告消息,现在presentedViewController 设置正确。
beforeEach(^{
mockUserDefaults = mock([NSUserDefaults class]);
loadingVC = [[ETLoadingVC alloc] initWithUserDefaults:mockUserDefaults];
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = loadingVC;
[window makeKeyAndVisible];
[loadingVC viewDidAppear:NO];
});
【问题讨论】:
标签: ios unit-testing bdd viewdidappear