我正在使用Specta来创建一些测试,但我似乎无法获得这个基本的通过。该应用程序本身工作正常,但这个测试不会通过。单元测试不叫viewDidAppear方法
的ViewController
- (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
,但我只是无法得到它的工作。
我的视图控制器都是用代码编写的。没有故事板或笔尖。
UPDATE
现在我添加了获取与类名即将呈现更新的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
仍然等于零即使这个更新的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];
});