Testing
▶ 테스트
1. Moya has been created with testing at its heart.
▶ Moya는 테스트를 중심으로 만들어졌습니다.
2. In this document, you will find all the customization points that will allow you to tailor your testing.
▶ 이 문서에서는 테스트를 맞춤화할 수 있는 모든 사용자 지정 지점을 찾을 수 있습니다.
sampleData
3. When creating your TargetType you are required to provide sampleData for your targets.
▶ TargetType을 만들 때, 대상에 대한 sampleData를 제공해야 합니다.
4. All you need to do there is to provide Data that represents a sample response from every particular target.
▶ 모든 특정 target의 sample response를 나타내는 Data를 제공하기만 하면 됩니다.
5. This can be used later for tests or just for providing offline support while developing.
▶ 이것은 나중에 테스트를 위해 또는 개발하는 동안 오프라인 지원을 제공하기 위해 사용할 수 있습니다.
For example:
▶ 예를 들어:
public var sampleData: Data {
switch self {
case .userRepositories(let name):
return "[{\"name\": \"Repo Name\"}]".data(using: String.Encoding.utf8)!
}
}
stubClosure
6. The stubClosure property of a MoyaProvider is responsible for configuring whether a provider returns an actual or a stubbed response.
▶ MoyaProvider의 stubClosure 속성은 provider가 실제 또는 스텁된 응답을 반환할지 여부를 구성하는 역할을 합니다.
7. This configuration is made in this closure by mapping a TargetType to a case of the StubBehavior enum:
- .never : to not stub responses.
- .immediate : to stub responses immediately.
- .delayed(seconds: TimeInterval) : to stub responses after a given delay (to simulate the delays the real network calls may have).
▶ 이 구성은 TargetType을 StubBehavior 열거형의 경우에 매핑하여 이 클로저에서 이루어집니다.
- .never : stub 응답을 하지 않습니다.
- .immediate : 즉시 응답을 스텁합니다.
- .delayed(seconds: TimeInterval) : 주어진 지연 후 응답을 스텁합니다 (실제 네트워크 호출이 있을 수 있는 지연을 시뮬레이션하기 위해).
8. For your convenience, MoyaProvider defines the .immediatelyStub and .delayedStub(:) closures that you can set while initializing your provider to always stub responses immediately or with a given delay.
▶ 당신의 편의를 위해, MoyaProvider는 항상 즉시 또는 지정된 지연으로 response를 스텁하도록 provider를 초기화하는 동안, 설정할 수 있는 .immediatelyStub 및 .delayedStub(:) 클로저를 정의합니다.
9. For example, in the following provider all your requests are going to receive immediate responses with your sampleData.
▶ 예를 들어, 다음 provider에서 모든 requests는 sampleData와 함께 즉각적인 response을 받게 됩니다.
let stubbingProvider = MoyaProvider<GitHub>(stubClosure: MoyaProvider.immediatelyStub)
10. Before you continue, it is worth mentioning that all the stubbed responses will be delivered with an HTTP status 200 by default.
sampleResponseClosure
11. With the previous sampleData and stubClosure, we could only specify the data returned when stubbing.
▶ 이전 sampleData 및 stubClosure에서는 스터빙 시, 반환되는 데이터만 지정할 수 있었습니다.
12. But you have more options.
▶ 하지만 더 많은 옵션이 있습니다.
13. Moya offers you the opportunity to configure an endpointClosure on your provider.
▶ Moya는 provider에서 endpointClosure를 구성할 수 있는 기회를 제공합니다.
14. In this closure, your Target needs to be mapped to an Endpoint.
▶ 이 클로져에서는 Target를 Endpoint에 매핑해야 합니다.
15. Endpoint is a semi-internal data structure used by Moya to reason about the request.
▶ Endpoint는 Moya에서 요청을 추론하는 데 사용하는 반? 내부 데이터 구조입니다.
16. And in this Endpoint is where you are going to be able to specify more details for your testing. More concretely, on its sampleResponseClosure.
▶ 그리고 이 Endpoint에서 테스트에 대한 세부정보를 더 지정할 수 있습니다. 더 구체적으로 말하자면, sampleResponseClosure에서.
17. As we discussed above, the default stubbing behavior is to respond to requests with your sample data with a 200 HTTP status code.
▶ 위에서 논의한 것처럼, default 스터빙 동작은 200 HTTP 상태 코드를 사용하여, Sample Data로 요청에 응답하는 것입니다.
18. This is because the default endpointClosure defines its sampleResponseClosure as follows:
▶ 이는 기본 endpointClosure가 sampleResponseClosure를 다음과 같이 정의하기 때문입니다.
{ .networkResponse(200, target.sampleData) }
19. If you need to setup your own sampleResponseClosure, your implementation should return a case of the EndpointSampleResponse enum:
- A .networkResponse(Int, Data) where Int is a status code and Data is the returned data. Useful to customize either response codes or data to your own specification.
- A .response(HTTPURLResponse, Data) where HTTPURLResponse is the response and Data is the returned data. Useful to fully stub your responses.
- A .networkError(NSError) where NSError is the error that occurred when sending the request or retrieving a response. Useful to test for any network errors: Timeouts, reachability issues, etc.
▶ 자체 sampleResponseClosure를 설정해야 하는 경우, 구현에서 EndpointSampleResponse 열거형의 case를 반환해야 합니다.
- A .networkResponse(Int, Data) : 여기서 Int는 상태 코드이고, Data는 반환된 데이터입니다. 응답코드 또는 데이터를 고유한 사양으로 사용자 정의하는 데 유용합니다.
- A .response(HTTPURLResponse, Data) : 여기서 HTTPURLResponse는 응답이고, Data는 반환된 데이터입니다. 응답을 완전히 스텁하는 데 유용합니다.
- A .networkError(NSError) : 여기서 NSError는 요청을 보내거나 응답을 검색할 때 발생한 오류입니다. 네트워크 오류 테스트에 유용: 시간 초과, 연결 문제 등
20. For example, the following code creates a provider to stub with immediate 401 responses:
let customEndpointClosure = { (target: APIService) -> Endpoint in
return Endpoint(url: URL(target: target).absoluteString,
sampleResponseClosure: { .networkResponse(401 , /* data relevant to the auth error */) },
method: target.method,
task: target.task,
httpHeaderFields: target.headers)
}
let stubbingProvider = MoyaProvider<GitHub>(endpointClosure: customEndpointClosure, stubClosure: MoyaProvider.immediatelyStub)
출처
GitHub - Moya/Moya: Network abstraction layer written in Swift.
Network abstraction layer written in Swift. Contribute to Moya/Moya development by creating an account on GitHub.
github.com
'Moya 문서' 카테고리의 다른 글
[한글 번역] Moya - Basic Usage (0) | 2022.06.08 |
---|---|
[한글 번역] Moya Readme.md - Usage (2) (0) | 2022.06.08 |
[한글 번역] Moya Readme.md / 개요 (1) (0) | 2022.06.08 |
댓글