简体   繁体   English

单元测试不要调用viewDidAppear方法

[英]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.

相关问题 iOS:单元测试看不到我的目标标识符 - iOS : Unit tests don't see my target identifiers XCTestExpectation在单元测试中正确执行方法调用的时间/位置? - Correct timing/placement of XCTestExpectation fulfill method call in unit tests? 为什么在嵌套结构中未调用viewDidAppear / viewWillAppear? - Why viewDidAppear/viewWillAppear don't get called in a nested structure? NSTimer不要在选择器中调用方法 - NSTimer don't call the method in selector 为什么不调用UIViewControllerTransitioningDelegate的方法presentationControllerForPresentedViewController? - Why don't call method presentationControllerForPresentedViewController of UIViewControllerTransitioningDelegate? 为什么不进行背景和重新打开应用程序调用viewDidAppear? - Why doesn't backgrounding and re-opening an app call viewDidAppear? Swift单元测试mock类静态方法 - Swift unit tests mock class static method 是否有任何用于UIView的viewDidAppear方法(不是UIViewController)? - Is there any viewDidAppear method for UIView (not UIViewController)? 无法在iOS设备上运行单元测试 - Can't run unit tests on iOS device 当UITabBarController在屏幕上显示第一个选项卡时,调用什么方法? viewDidAppear不起作用 - What method is called when UITabBarController shows first tab on screen? viewDidAppear doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM