Repository: Moya/Moya
File: Tests/MoyaProviderIntegrationTests.swift (Swift)

Code before:

                it("returns some data for user profile request") {
                    var message: String?

                    waitUntil { done in
                        let target: GitHub = .userProfile("ashfurrow")
                        provider.reactive.request(target).startWithResult { result in
                            if case .success(let response) = result {
                                message = String(data: response.data, encoding: String.Encoding.utf8)
                                done()
                            }
                        }
                    }

                    expect(message) == userMessage
                }
            }
        }

        // Resolves ValidationType not working with multipart upload #1590
        describe("a provider performing a multipart upload with Alamofire validation") {
            it("only allows status code 287") {
                let formData = HTTPBin.createMultipartFormData()
                let target = HTTPBin.validatedUploadMultipart(formData, nil)
                let provider = MoyaProvider<HTTPBin>()
                provider.request(target) { result in
                    switch result {
                    case .success(let response):
                        let validCodes = target.validationType.statusCodes
                        expect(validCodes).to(contain(response.statusCode))
                    case .failure(let error):
                        switch error {
                        case .underlying(_, let response):
                            let validCodes = target.validationType.statusCodes
                            expect(validCodes).toNot(contain(response!.statusCode))
                        default:
                            fatalError("Received unexpected error")
                        }
                    }
                }
            }
        }
    }
}

final class StubManager: Manager {
    var called = false

    override func request(_ urlRequest: URLRequestConvertible) -> DataRequest {
        called = true
        return super.request(urlRequest)

Review comment (suggestion):
I would restructure this testing code a little bit. Instead of using `expect`s inside the closures, we could use `waitUntil` structure. This makes sure we don't skip this test, where with using `expect`s inside the closure we are risking the situation where this test is skipped, e.g. when closure is not executed. See tests above for examples.

Code after:

                it("returns some data for user profile request") {
                    var message: String?

                    waitUntil { done in
                        let target: GitHub = .userProfile("ashfurrow")
                        provider.reactive.request(target).startWithResult { result in
                            if case .success(let response) = result {
                                message = String(data: response.data, encoding: String.Encoding.utf8)
                                done()
                            }
                        }
                    }

                    expect(message) == userMessage
                }
            }
        }

        // Resolves ValidationType not working with multipart upload #1590
        describe("a provider performing a multipart upload with Alamofire validation") {
            let provider = MoyaProvider<HTTPBin>()
            let formData = HTTPBin.createTestMultipartFormData()

            it("returns an error for status code different than 287") {
                let target = HTTPBin.validatedUploadMultipart(formData, nil, [287])
                var receievedResponse: Response?
                var receivedError: Error?

                waitUntil(timeout: 5.0) { done in
                    provider.request(target) { result in
                        switch result {
                        case .success(let response):
                            receievedResponse = response
                        case .failure(let error):
                            receivedError = error
                        }
                        done()
                    }
                }

                expect(receievedResponse).to(beNil())
                expect(receivedError).toNot(beNil())
            }

            it("returns a valid response for .succesCodes") {
                let successCodes = ValidationType.successCodes.statusCodes
                let target = HTTPBin.validatedUploadMultipart(formData, nil, successCodes)
                var receievedResponse: Response?
                var receivedError: Error?