【发布时间】:2019-01-01 08:54:16
【问题描述】:
我有一个服务类,我想断言 2 件事
- 方法被调用
- 正确的参数被传递给该方法
这是我的课
protocol OAuthServiceProtocol {
func initAuthCodeFlow() -> Void
func renderOAuthWebView(forService service: IdentityEndpoint, queryitems: [String: String]) -> Void
}
class OAuthService: OAuthServiceProtocol {
fileprivate let apiClient: APIClient
init(apiClient: APIClient) {
self.apiClient = apiClient
}
func initAuthCodeFlow() -> Void {
}
func renderOAuthWebView(forService service: IdentityEndpoint, queryitems: [String: String]) -> Void {
}
}
这是我的测试
class OAuthServiceTests: XCTestCase {
var mockAPIClient: APIClient!
var mockURLSession: MockURLSession!
var sut: OAuthService!
override func setUp() {
mockAPIClient = APIClient()
mockAPIClient.session = MockURLSession(data: nil, urlResponse: nil, error: nil)
sut = OAuthService(apiClient: mockAPIClient)
}
func test_InitAuthCodeFlow_CallsRenderOAuthWebView() {
let renderOAuthWebViewExpectation = expectation(description: "RenderOAuthWebView")
class OAuthServiceMock: OAuthService {
override func initAuthCodeFlow() -> Void {
}
override func renderOAuthWebView(forService service: IdentityEndpoint, queryitems: [String: String]) {
renderOAuthWebViewExpectation.fulfill()
}
}
}
}
我希望创建一个 OAuthService 的本地子类,将其指定为我的 sut 并调用类似 sut.initAuthCodeFlow() 之类的名称,然后断言我的期望已实现。
我认为这应该满足第 1 点。但是,当我尝试将其分配为已满足时,我无法访问我的期望,因为我收到以下错误
类声明不能关闭值 在外部范围中定义的“renderOAuthWebViewExpectation”
如何将其标记为已完成?
我采用的是 TDD 方法,所以我知道我的 OAuthService 在这一点上无论如何都会产生一个失败的测试*
【问题讨论】:
标签: swift unit-testing tdd xctestcase