关于第三方单元测试框架的简单说明
Pod安装
Podfile编写(框架只能安装在UnitTest的Target中)
1 2 3 4 5 6 7 8 9
| target 'BaZiPaiPanSDK' do ... target 'BaZiPaiPanSDKTests' do inherit! :search_paths pod 'Specta' pod 'Expecta' pod 'OCMock' end end
|
Specta
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
SpecBegin(BZPPCaiYunViewModel) // 声明了一个测试类
__block BZPPCaiYunViewModel *viewModel; // 我们要测试这个类
beforeEach(^{ // 通过beforeEach 首先先初始化 viewModel = [[BZPPCaiYunViewModel alloc] init]; });
describe(@"BZPPCaiYunViewModel", ^{ // 声明一组用例 it(@"refresh when data isn't empty", ^{ // 通过 it 声明一个用例,前面的字符串是一个描述 __block id result; waitUntil(^(DoneCallback done) { // 假设这里有网络请求,所以需要异步,有点像GCD的信号量 [[viewModel.loadDataCommand execute:nil] subscribeNext:^(id x) { result = x; done(); // 请求结束,告知框架可以继续往下走 }]; }); expect(result).notTo.beNull(); // 这里是Expecta框架的内容,后面解释 }); });
afterEach(^{ viewModel = nil; // 测试结束,清空 });
SpecEnd // 类的结束,相当于 @end
|
OCMock
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| SpecBegin(BZPPCaiYunViewModel)
__block BZPPCaiYunViewModel *viewModel
beforeEach(^{ })
describe(@"Test OCMock", ^{ it(@"It should not to be null", ^{ id mockManager = OCMClassMock([BZPPHunLianManager class]) OCMStub([mockManager sharedInstance]).andReturn([BZPPHunLianManager class]) OCMVerify([mockManager getGanQingHunYinData:OCMOCK_ANY isMale:YES]) }) })
afterEach(^{ })
SpecEnd
|
Expecta
- Expecta是一个断言框架, 语法十分地通俗易懂, 很接近自然语言
- Expecta的用法比较简单
1 2 3
| expect(obj).to.beNull(); expect(obj).toNot.beNull(); expect(obj).equal(xxx);
|
诸如此类比较多可以去看Github官方文档