Swift 中的 OCMVerify/Expect? [英] OCMVerify/Expect in Swift?

查看:19
本文介绍了Swift 中的 OCMVerify/Expect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单元测试中验证在 Swift 中是否使用正确的参数调用了预期的方法.这在使用 OCMock 框架的 Objective-C 中很容易完成.您可以结合使用 partialMocking 和运行 OCMExpect/Verify 来断言使用正确参数调用的代码路径.知道如何在 Swift 中做这样的事情吗?

I would like to Verify that an expected method is called with the correct parameters in Swift in unit-testing. This was very easily done in Objective-C using the OCMock framework. You could do a combination of partialMocking and running OCMExpect/Verify to assert code paths with the right parameters were being called. Any idea how to do something like this in Swift?

推荐答案

Swift 不支持反射,因此传统的模拟库不可行.相反,您需要创建自己的模拟.至少有两种方法可以做到这一点.

Swift doesn't support reflection, so traditional mocking libraries aren't feasible. Instead, you need to create your own mock. There are at least two approaches to do this.

  1. 创建被测类的测试子类.这是部分嘲讽.如果可能,请避免这种情况.
  2. 使用接口而不是类.创建接口的测试实现.

手工制作的模拟并不难.你想

Hand-crafted mocks aren't hard. You want to

  • 计算方法调用次数
  • 捕捉它的论点
  • 模拟它的返回值

这是很多样板文件.有一些库可以自动生成此代码.

It is a lot of boilerplate. There are libraries out there that can auto-generate this code.

这篇关于Swift 中的 OCMVerify/Expect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆