第一次用还有点费劲
经过两个多月的使用发现还是挺方便的,也学到了不少invocation的东西,但是总感觉测试有点事后诸葛亮的味道,尤其是当前对着已有的代码写测试用例,还没有get到它的威力,还是TDD开发,啥时候试试看。
里程碑文件
Faking network calls for iOS unit tests
使用
网络模拟
这里说下自己开始遇到的一个问题,网络请求可能分装了很多层,如果想要覆盖更多的代码,就要让接口请求尽量在更外层,真正mock替换的方法尽量在最内层。
由以下几步组成
- 找到网络请求发起的动作 request
- 找到网络请求返回的动作 response
- OCMExpect(response).andDo(),替换 mock data
- OCMStub(request).andCall(response),直接转到 mock 返回
- 然后用mock请求接口就好了
// 模拟 af manager |
Notification通知模拟
新的 ocmock 已经完全用 XCTNSNotificationExpectation 来代替就方法了。
// 1.能验证收到通知,但是会真的继续执行 post notification 后面的方法 |
问题
开始卡住的地方有两个;然后陆续发现一些别的。
stub expect 区别
Using OCMock to create iOS Unit Tests
Both –stub and –expect allow you to force a method to return a certain value whenever called during your test so you don’t have to worry about its result. In the example above, I am forcing the getPlaceName to return @”JFK International Airport”.
So, what is the difference between –stub and –expect?
The main difference is: you –expect methods that must happen, and –stub methods that might happen.
There are 2 ways mock objects fail: either an unexpected/unstubbed method is called, or an expected method is not called:
- Unexpected invocations. When a mock object receives a message that hasn’t been either stubbed or expected, it throws an exception immediately and your test fails.
- Expected invocations. When you call verify on your mock (generally at the end of your test), it checks to make sure all of the methods you expected were actually called. If any were not, your test will fail.
verify private method
虽然还在找 verify 私有(没有声明在h文件里的)方法,但是这句话值得记下来。
找到了Unit testing private method - objective C
If a method is private, you never should test it.
Think about this. You should test behaviour and contract of your methods instead internal implementation
模拟的data在哪里替换
尤其是看了很多例子都是 block 方式来替换的,但是 delegate 的方法到底如何才能调用/替换?尤其是data要在哪个步骤模拟?
- 可以在 delegate 的 onfinished 替换,我选择了这里。
- 可以在 requester 的 onfinished 替换?
//********************************************************** |
如何立刻让网络请求返回到第一步模拟的 onfinished
写完一个满足条件后,直接就发现,如果不让网络请求发生而直接返回结果?
其实想想肯定有个地方需要指定这个关系,一请求就立马执行返回函数,也就是这里
//4. the mock object that will mock a download/ApiUtility |
怎么跳过或者处理每次都执行的 didFinishLaunchingWithOptions
测试其他方法的时候,每次都要执行 launch 流程,导致 launch 相关的接口都会执行一遍,而这些接口要怎么模拟?
参考
ocmock 方面
Improve Unit Test with OCMock
Unit testing private method - objective C
OCMock使用实例
Kiwi 使用进阶 Mock, Stub, 参数捕获和异步测试
iOS测试系列(四):Specta,Expecta和Kiwi的使用
ocmock-cheatsheet.m
Using OCMock to create iOS Unit Tests
How to mock response from AFNetworking with OCMock in Swift?
Faking network calls for iOS unit tests
iOS Unit Testing - Switching method implementations with OCMock (Part 3)
iOS开发 | 如何为网络接口编写单元测试
为网络接口编写单元测试(OCMock注入)
Reference
ocmock 之外 uitest
Three ways UI Testing just made test-driven development even better
Recording Your XCTests on CircleCI
iOS — Separation of XCUnit & XCUITest jobs in CircleCI for single & multi app regions/variants
如果几十万的评论模拟,mock json 也太难搞了看下这个
PICK一下,iOS自动化测试新方案出道