简体   繁体   English

开关盒的iOS单元测试

[英]iOS Unit Test on switch case

I'm a beginner in Unit Test and I would like to test my cases in a switch but I don't know how to do it. 我是单元测试的初学者,我想在交换机中测试我的案例,但是我不知道该怎么做。

I have : 我有 :

 - (void)testClickSmiley
{
    [self.viewController click:nil];
    // Here What i do ? I use what kind of XCTest Assertions ? I want to test if it goes into "default" for example
}

And in my ViewController : 在我的ViewController中:

- (IBAction)click:(id)sender
{
    UIButton *btn = (UIButton *)sender;

    switch (btn.tag) {

        case Bad:
            // Show view Bad
            break;

        case Average:
            // Show view Average
            break;

        case Good:
            // Show view Bad
            break;

        default:
            break;
    }
}

Of course, I don't want to modify my ViewController. 当然,我不想修改ViewController。

Any ideas ? 有任何想法吗 ? TY TY

What you actually should be doing in this case is writing UI tests for this scenario. 在这种情况下,您实际上应该做的是为此场景编写UI测试。 Your context and execution environment do not allow you to test your code based on unit tests (for example, the app is not aware of any button you pass to the test) the way you expect it. 您的上下文和执行环境不允许您以期望的方式基于单元测试来测试代码(例如,应用程序不知道传递给测试的任何按钮)。

Of course the first thing that is wrong is that you use 当然,第一件事是您使用

[self.viewController click:nil];

The click function will get a nil value for the button and the tag will therefore be nil as well. click函数将获得按钮的nil值,因此标记也将为nil。

Of course you could mock a button: 当然,您可以嘲笑一个按钮:

UIButton *button = [[UIButton alloc] initWith...]
button.tag = [YourEnum].Bad
[self.viewController click: button];

But that would still leave you with the problem that you don't know where the switch ended up going... 但这仍然会给您带来一个问题,即您不知道开关的最终位置...

Solution (if applicable): 解决方案(如果适用):

Take a look at UI Testing 看看UI测试

https://developer.apple.com/videos/play/wwdc2015/406/ https://developer.apple.com/videos/play/wwdc2015/406/

It allows you to run the application and simulate user interactions + you have the benefit that you can always assume you are working with the actual button that caused the click: event in the first place. 它使您可以运行应用程序并模拟用户交互,并且您可以始终假定自己使用的是导致click:事件的实际按钮。

There is nothing wrong with exercising your view controller in a straight unit test without using UI testing. 在不使用UI测试的情况下,在直接的单元测试中锻炼视图控制器没有错。 In fact, I would have more unit tests and fewer UI tests (if any). 实际上,我会有更多的单元测试和更少的 UI测试(如果有)。

Why? 为什么? It depends on the purpose of your tests. 这取决于测试的目的。 The reason I test is to get fast feedback to enable refactoring and TDD. 我测试的原因是获得快速反馈以启用重构和TDD。 I need tests that are fast and dependable, not slow and fragile. 我需要快速而可靠的测试,而不是缓慢而脆弱的测试。

So go ahead and write tests that invoke your view controller. 因此,继续编写调用您的视图控制器的测试。 To your question, "What do I do here?" 对于您的问题,“我在这里做什么?” You verify the actions that would be taken. 您验证将要执行的操作。 For example, you can test 例如,您可以测试

  • Changes to the view 更改视图
  • Changes to the underlying model 对基础模型的更改
  • Calling the next view controller with the expected data 用期望的数据调用下一个视图控制器

It's unusual to have a single IBAction method act as a fan-out of multiple actions. 单个IBAction方法充当多个动作的扇出是不寻常的。 This ties them together unnecessarily. 这将它们不必要地联系在一起。 A change to a single action could break other actions. 更改单个动作可能会破坏其他动作。 Instead, consider creating multiple IBAction methods, one per action . 相反,可以考虑创建多个IBAction方法,每个操作一个

To see an example of how to write unit tests for a UIViewController — in fact, how to TDD it — see my screencast How to Do UIViewController TDD . 要查看有关如何为UIViewController编写单元测试(实际上是如何进行TDD)的示例,请参阅我的截屏视频如何做UIViewController TDD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM