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)