单元测试不要调用viewDidAppear方法 [英] Unit tests don't call viewDidAppear method
问题描述
我正在使用Specta创建一些测试,但是我似乎无法通过此基本测试.该应用程序本身可以正常运行,但此测试无法通过.
I am using Specta to create some tests but I don't seem to be able to get this basic one to pass. The app itself works fine but this test won't pass.
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
This fails with: the actual value is nil/null
我尝试致电:
[loadingVC view]
我什至已经启动了UIWindow
和appDelegate
,但是我无法使其正常工作.
I've even initiated a UIWindow
and an appDelegate
but I just can't get it working.
我的视图控制器全部用代码编写.没有情节提要或笔尖.
My view controller is all written in code. No storyboards or nibs.
更新
现在,我添加了一个NSString
属性,该属性将使用即将显示的类名进行更新.然后,我在测试中检查该字符串.为了使此工作正常进行,我必须将beforeEach
块更改为以下内容:
For now i've added an NSString
property that gets updated with the class name that is about to be presented. I then check this string in my test. To get this working though, I had to change my beforeEach
block to the following:
beforeEach(^{
loadingVC = [[ETLoadingVC alloc] initWithUserDefaults:nil];
[loadingVC viewDidAppear:NO];
});
尽管测试通过了,但我收到以下消息:
Although the test passes, I get the following message:
Warning: Attempt to present <ETLoginVC: 0x7fcf34961940> on <ETLoadingVC: 0x7fcf34961280> whose view is not in the window hierarchy!
我知道这是因为我在当前视图完成显示之前正在调用viewDidAppear
.我没有得到的是如何测试这种更好的方法.
I get that this is because i'm calling viewDidAppear
before the current view has finished appearing. What I don't get is how I can test this a better way.
即使在这个更新的beforeEach
块中,我也不明白为什么loadingVC.presentedViewController
仍然等于nil.
I also don't understand why loadingVC.presentedViewController
still equals nil even with this updated beforeEach
block.
更新2
将beforeEach
更改为以下内容摆脱了警告消息,现在presentedViewController
已正确设置.
Changing the beforeEach
to the below got rid of the warning message and now the presentedViewController
is set correctly.
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];
});
推荐答案
将beforeEach
更改为以下内容似乎可以解决我的问题.
Changing the beforeEach
to the following seemed to solve my problem.
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];
});
这篇关于单元测试不要调用viewDidAppear方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!