简体   繁体   English

如何对UITableView进行单元测试?

[英]How to unit test UITableView?

I am trying to unit test UITableView but I have no idea how to do it. 我正在尝试单元测试UITableView,但我不知道该怎么做。 I have a view controller that has a table view in it. 我有一个视图控制器,其中有一个表视图。 Each prototype cell has a custom table view cell class with outlets attached to it. 每个原型单元都有一个自定义表视图单元类,其上连接有插座。

Now, I want to test both of these components separately. 现在,我想分别测试这两个组件。 Firstly, I want to test if the data inside the table view cell is correct by creating the prototype cell separately. 首先,我想通过单独创建原型单元来测试表视图单元格内的数据是否正确。 Secondly, I want to populate my table view with random data and test whether the objects are correct etc. How can I do both of these? 其次,我想用随机数据填充我的表视图并测试对象是否正确等。我怎样才能做到这两个?

For testing the cell itself, I have no idea how to create the cell separately and check it by sending data to it. 为了测试单元本身,我不知道如何单独创建单元并通过向其发送数据来检查它。 So, my custom class has small functions to set data to cell instead of directly assigning data to label, button, image. 因此,我的自定义类具有将数据设置为单元格的小功能,而不是直接将数据分配给标签,按钮,图像。 I want to test those functions. 我想测试那些功能。 But I can't check it if I am not rendering it. 但是,如果我没有渲染它,我无法检查它。

For testing the components, my problem is different in the sense that, I want to change the data that is coming to it. 为了测试组件,我的问题是不同的,我想改变即将发生的数据。 So, instead of reading the data from API, I want to read it normally but I don't know how to change viewDidLoad to work with local objects instead of network objects. 因此,我不想从API中读取数据,而是想要正常读取它,但我不知道如何更改viewDidLoad以使用本地对象而不是网络对象。

The short answer is: you don't. 简短的回答是:你没有。 As a general rule of thumb, in unit testing you should be testing the business logic, not the UI. 作为一般经验法则,在单元测试中,您应该测试业务逻辑,而不是UI。 Views, in general, should be so void of functionality that there is no reason to run unit tests against them. 通常,视图应该没有功能,因此没有理由对它们运行单元测试。

Edit : to expound on this a bit more. 编辑 :对此进行详细阐述。 I prefer to only store information in a view that's related to how it looks, where's it's positioned, etc. This can usually be programmed declaratively In addition, the view will have the smallest functionality necessary to convert user interaction from your UI framework into an interface for your presenter (if using MVP) or controller (if using MVC). 我更喜欢只在视图中存储信息,这些信息与它的外观,位置等有关。这通常可以声明性地编程。此外,视图将具有将用户交互从UI框架转换为界面所需的最小功能为您的演示者(如果使用MVP)或控制器(如果使用MVC)。 The presenter or controller could then be tested to your heart's content through the same interface. 然后,可以通过相同的界面对演示者或控制器进行心脏测试。

I also find you can avoid a lot of complexity and bugs if you develop a preference for creating new views rather than rerendering existing views. 我还发现,如果您开发创建新视图的首选项而不是重新呈现现有视图,则可以避免很多复杂性和错误。 This isn't always feasible. 这并不总是可行的。 But when it is, the view has do a lot less dynamic work and instead just show the model that would have been passed into its constructor. 但是当它出现时,视图的动态工作要少得多,而只是显示传递给构造函数的模型。

I'm too facing the same problem.. and till now haven't found any solution..!! 我也面临同样的问题......直到现在还没有找到任何解决方案.. !!

but I got this link < unit tests with TableView > 但我得到了这个链接<使用TableView进行单元测试>

Though it is in Objective-C I mean the code.. but once you read the code you, yourself will be able to write the same in Swift 3.. else you can try online converters.. 虽然它是在Objective-C中我的意思是代码..但是一旦你读了代码,你自己就可以在Swift 3中编写相同的代码..否则你可以尝试在线转换器..

Also by referring the above article I'm able to write few Test Cases which are working fine.. 另外通过参考上面的文章,我可以编写一些工作正常的测试用例。

Hope it helps. 希望能帮助到你。

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

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