Xcode iOS 中的单元测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15643480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:04:02  来源:igfitidea点击:

Unit Test in Xcode iOS

iosobjective-cxcodeunit-testingtdd

提问by Fernando

I'm not so experience in Objective-C, but I'm I'd like to start develop in iOS using TDD, for this I have few newbie questions :)

我在 Objective-C 方面没有太多经验,但我想开始使用 TDD 在 iOS 中进行开发,为此我有几个新手问题:)

What people are using for TDD for iOS? I was think in use the Test framework that already have in Xcode, is it good? or any better options?

人们在 iOS 上使用什么 TDD?我认为在使用Xcode中已有的Test框架,它好吗?或任何更好的选择?

I was trying to do some exercises for unit test, and I was creating an Example, where I have a TableViewControllerClass, using CoreLocation, with this methods:

我试图为单元测试做一些练习,我正在创建一个示例,其中我有一个 TableViewControllerClass,使用 CoreLocation,使用以下方法:

    -(void)startFindLocation
    {
        [self.cllManager startUpdatingLocation];
    }

    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        [self.cllManager stopUpdatingLocation];
        CLLocation *actualLocation = [locations lastObject];

        NSLog(@"latitude: %f", actualLocation.coordinate.latitude);
        NSLog(@"longitude: %f", actualLocation.coordinate.longitude);
    }

What I would like to test, is very simple, is to see if the CLLocation has a latitude and a longitude, instead of using NSLong. For that I really need to alloc and init this TableViewController class in my Test Class? or there's any framework, similar to Rspec (for example, ruby/rails), where I can check if a specific label in the application screen has this value of latitude and longitude?

我想测试的,很简单,就是看CLLocation有没有经纬度,而不是用NSLong。为此,我真的需要在我的测试类中分配和初始化这个 TableViewController 类吗?或者有任何框架,类似于 Rspec(例如,ruby/rails),我可以在其中检查应用程序屏幕中的特定标签是否具有此纬度和经度值?

Regards

问候

采纳答案by Marin Todorov

Follow through on this tutorial to get the basics: http://www.raywenderlich.com/3716/unit-testing-in-xcode-4-quick-start-guide

按照本教程获取基础知识:http: //www.raywenderlich.com/3716/unit-testing-in-xcode-4-quick-start-guide

Then you'll be able to decide how to write your own test suite. Good luck

然后,您将能够决定如何编写自己的测试套件。祝你好运

回答by Jon Reid

I have a series of screencasts on iOS TDD, starting with https://qualitycoding.org/objective-c-tdd/

我有一系列关于 iOS TDD 的截屏视频,从https://qualitycoding.org/objective-c-tdd/开始

Regarding your particular question, a unit test needs to check results in one of three ways:

关于您的特定问题,单元测试需要通过以下三种方式之一检查结果:

  1. Return value verification
  2. State verification
  3. Behavior verification
  1. 返回值验证
  2. 状态验证
  3. 行为验证

I don't see anything in -locationManager:didUpdateLocations: that leads to any of these. Perhaps you want to TDD a helper method instead.

我在 -locationManager:didUpdateLocations: 中没有看到任何导致这些的任何内容。也许你想用 TDD 一个辅助方法来代替。