相关疑难解决方法(0)

在Swift中测试断言

我正在为一个有断言的方法编写单元测试.Swift语言指南建议对"无效条件"使用断言:

断言导致您的应用程序终止,并且不能替代以不可能出现无效条件的方式设计代码.尽管如此,在可能出现无效条件的情况下,断言是确保在应用程序发布之前在开发过程中突出显示并注意到这些条件的有效方法.

我想测试失败案例.

但是,XCTAssertThrowsSwift中没有(从Beta 6开始).如何编写测试断言失败的单元测试?

编辑

根据@ RobNapier的建议,我尝试XCTAssertThrows用Objective-C方法包装并从Swift调用这个方法.这不起作用,因为宏没有捕获assert由此引起的致命错误,因此测试崩溃.

unit-testing xctest swift

24
推荐指数
4
解决办法
8550
查看次数

How to assert an error is thrown async when testing?

We can test thrown errors with XCTAssertThrowsError. Async things can be tested with expectation. I have some method which dispatch work to a background thread and can at some point throw an error.

Is it possible to expect an error be thrown somewhere in the future? I need to combine expectation and XCTAssertThrowsError I think, but I do not know how.

复制项目: https: //github.com/Jasperav/ThrowingAsyncError。只需克隆项目并运行测试,其中之一就会失败。我创建了一个类,该类在分配后几秒钟后就会崩溃。我想确保它在几秒钟后继续崩溃,所以我想要一个测试用例。

unit-testing xctest swift

17
推荐指数
2
解决办法
4611
查看次数

标签 统计

swift ×2

unit-testing ×2

xctest ×2