[英]Unit tests don't call viewDidAppear method
I am using Specta to create some tests but I don't seem to be able to get this basic one to pass. 我正在使用Specta创建一些测试,但似乎无法通过这一基本测试。 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]);
}
This logs: PresentedVC: ETLoginVC
此日志:
PresentedVC: ETLoginVC
Specs 眼镜
#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
This fails with: the actual value is nil/null
这将失败:
the actual value is nil/null
I've tried calling: 我试过打电话给:
[loadingVC view]
I've even initiated a UIWindow
and an appDelegate
but I just can't get it working. 我什至已经启动了一个
UIWindow
和一个appDelegate
但是我无法使其正常工作。
My view controller is all written in code. 我的视图控制器都是用代码编写的。 No storyboards or nibs.
没有情节提要或笔尖。
UPDATE UPDATE
For now i've added an NSString
property that gets updated with the class name that is about to be presented. 现在,我添加了一个
NSString
属性,该属性将使用即将显示的类名进行更新。 I then check this string in my test. 然后,在测试中检查此字符串。 To get this working though, I had to change my
beforeEach
block to the following: 为了使此工作正常进行,我必须将我的
beforeEach
块更改为以下内容:
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!
I get that this is because i'm calling viewDidAppear
before the current view has finished appearing. 我得到这是因为我在当前视图完成显示之前调用
viewDidAppear
。 What I don't get is how I can test this a better way. 我没有得到的是如何测试这种更好的方法。
I also don't understand why loadingVC.presentedViewController
still equals nil even with this updated beforeEach
block. 我也不明白为什么即使使用此更新的
beforeEach
块, loadingVC.presentedViewController
仍然等于nil。
UPDATE 2 更新2
Changing the beforeEach
to the below got rid of the warning message and now the presentedViewController
is set correctly. 将
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];
});
Changing the beforeEach
to the following seemed to solve my problem. 将
beforeEach
更改为以下似乎可以解决我的问题。
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];
});
Unit tests are more useful for testing objects and logic. 单元测试对于测试对象和逻辑更为有用。 You might want to look into integration testing, which is used to simulate actual UI behaviors.
您可能需要研究集成测试,该测试用于模拟实际的UI行为。 KIF is a framework that has been used a lot and is built on top of XCTest: http://www.raywenderlich.com/61419/ios-ui-testing-with-kif
KIF是已被广泛使用的框架,它建立在XCTest之上: http ://www.raywenderlich.com/61419/ios-ui-testing-with-kif
Everything is written inside xcode so you don't need any extra setup (unless you want to do continuous testing). 一切都是用xcode编写的,因此您不需要任何额外的设置(除非您想进行连续测试)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.