iOS:使用XCTest对UIViewController组件进行单元测试

时间:2022-04-18 20:39:51

I am implementing a unit test on the functionality of iOS UIButton in a view controller in Swift. The expected functionality of this button is as followed:

我正在Swift的视图控制器中实现iOS UIButton功能的单元测试。此按钮的预期功能如下:

  1. The button initially has a title of "Start"
  2. 该按钮最初的标题为“开始”
  3. After the button is pressed, the title changes to "Pause"
  4. 按下按钮后,标题将变为“暂停”

In short, I would like to test that the button's title is changed from "Start" to "Pause" after the button has been clicked.

简而言之,我想测试单击按钮后按钮的标题从“开始”更改为“暂停”。

Because the system needs a bit of time before the change takes place and is rendered on screen, I need to poll for a short amount of time to check the current title of the button until either the button title changes or the allotted time runs out.

由于系统在更改发生之前需要一些时间并在屏幕上呈现,因此我需要轮询一小段时间来检查按钮的当前标题,直到按钮标题更改或分配的时间用完为止。

Normally, I use Quick/Nimble framework for the unit testing and I use expect(...).toEventually(...) function to test this kind of polling. However, my team insists that I use XCTest framework for the sake of consistency, so I have to find some elegant way to implement the test with XCTest.

通常,我使用Quick / Nimble框架进行单元测试,我使用expect(...)。toEventually(...)函数来测试这种轮询。但是,我的团队坚持认为我使用XCTest框架是为了保持一致性,所以我必须找到一些优雅的方法来实现XCTest的测试。

Do you have any suggestions for the implementation with XCTest framework?

您对XCTest框架的实现有什么建议吗?

1 个解决方案

#1


0  

A solution that I can come up with is using NSTimer which triggers a condition check every small interval until the designated time runs out, and use XCTestExpectation and waitForExpectationsWithTimeout in order to wait for the completion. (See more about XCTestExpectation here)

我能想出的解决方案是使用NSTimer,它会在每个小间隔内触发条件检查,直到指定的时间用完为止,并使用XCTestExpectation和waitForExpectationsWithTimeout来等待完成。 (在此处查看有关XCTestExpectation的更多信息)

Are there any better ways to implement this unit test?

有没有更好的方法来实施这个单元测试?

#1


0  

A solution that I can come up with is using NSTimer which triggers a condition check every small interval until the designated time runs out, and use XCTestExpectation and waitForExpectationsWithTimeout in order to wait for the completion. (See more about XCTestExpectation here)

我能想出的解决方案是使用NSTimer,它会在每个小间隔内触发条件检查,直到指定的时间用完为止,并使用XCTestExpectation和waitForExpectationsWithTimeout来等待完成。 (在此处查看有关XCTestExpectation的更多信息)

Are there any better ways to implement this unit test?

有没有更好的方法来实施这个单元测试?