我正在为基于 Siesta 的课程编写测试,并尝试访问从服务器收到的错误。在我的对象中,我这样配置服务:
self.service.configure {
$0.config.pipeline[.parsing].add(SwiftyJSONTransformer, contentTypes: ["*/json"])
// other configuration setup
}
我的测试包含以下内容:
api.fetchApiToken(libraryRequiringAuth).onSuccess({ _ in
// Then
XCTFail("Expected request failure, got success")
}).onFailure({ [weak self] (error) in
XCTAssertEqual(error.httpStatusCode, expectedStatusCode)
let serverError: JSON? = error.entity?.typedContent()
XCTAssertEqual(serverError![0]["title"].string, expectedErrorMessage)
print("Expected error is: \(error)")
XCTAssertNil(self?.api.bearerAuthHeader)
expectation.fulfill()
})
该行let serverError: JSON? = error.entity?.typedContent()
设置serverError
为nil
,但在调试器中,我可以看到它error.entity
存在并且具有我期望的内容。我现在不能使用 SwiftyJSON 吗?
编辑:
以下是错误的内容:
Error Error(userMessage: "Forbidden", httpStatusCode: Optional(403), entity: Optional(Siesta.Entity(content: [{
ipRangeError = {
libraryId = 657;
libraryName = "Test library";
requestIp = "my.ip.address.was_here";
};
status = 403;
title = "Authentication Failed";
userData = {
};
}], charset: Optional("utf-8"), headers: ["content-type": "application/json; charset=utf-8"], timestamp: 490978565.82571602)), cause: nil, timestamp: 490978565.825499)