AlamofireMock
允许您在不使用自定义 URLProtocol 的情况下轻松进行模拟。使用自定义 URLProtocol 子类较为容易,但在进行测试时也涉及到异步操作。
使用示例
import XCTest
import AlamofireMock
class MyTest: XCTestCase
{
func testThingReturnsData()
{
let rawJson = "[{ \"group\":\"Research and Development\", \"name\":\"Engineering\" }]".data(using: .utf8)
let manager = SessionManagerMock(responseStore: DefaultResponseStore(data: rawJson))
let sut = SomeAPI(manager: manager)
let expection = XCTestExpectation(description: "Expecting items to be retrieved.")
sut.fetchDepartments { items in
XCTAssertEqual(items, 1)
XCTAssertEqual(items[0].group, "Research and Development")
XCTAssertEqual(items[0].name, "Engineering")
expection.fulfill()
}
// don't really need since all closure's are synchronous
wait(for: [expection], timeout: 1)
}
}