Swift 5.0 网络层单元测试

2019-09-29  本文已影响0人  聆听随风

原先写代码功能的测试,是通过点击UI,通过页面跳转,或者写测试按钮单独测试某项功能,这样测试缺点:

1. 测试麻烦;

2. 有些临界点不容易测出来;

通过网络层请求做的单元测试,把中间坑记录下:

1. 测试代码调用bug , 提示 No such module '' 

解决方案,需要倒入module,如下图:

   2. 网络层是异步执行,异步部分代码部分不执行

    解决方案:

调用代码之前写:

let expectationStr = expectation(description:"") 备注expectationStr 不能声明称expectation

let timeout = 30 as TimeInterval

回调部分代码:  expectationStr.fulfill()

代码最后部分写:waitForExpectations(timeout: timeout, handler:nil)

整体部分代码写法:

let healthModelInputViewModel = HealthModelInputViewModel.init()

        letexpectationStr =expectation(description:"")

        lettimeout =30asTimeInterval

        healthModelInputViewModel.getHealthModelInputContent(id:"1") { (resultCode, data, errorMsg)in

             expectationStr.fulfill()

             print("resultCode:", resultCode)

        }

        waitForExpectations(timeout: timeout, handler:nil)

        print("textExample")

参考文章:https://www.jianshu.com/p/be32739d8aae

                    https://stackoverflow.com/questions/32008403/no-such-module-when-using-testable-in-xcode-unit-tests

上一篇 下一篇

猜你喜欢

热点阅读