Skip to content

Instantly share code, notes, and snippets.

@Andrea-Scuderi
Created September 3, 2019 12:46
Show Gist options
  • Save Andrea-Scuderi/247522b550d2e0ba89c85d7cb2ce5c81 to your computer and use it in GitHub Desktop.
Save Andrea-Scuderi/247522b550d2e0ba89c85d7cb2ce5c81 to your computer and use it in GitHub Desktop.
Create XCTest
func testCreate() {
//Setup fixture
let usersURL = URL(string: APIDemo.baseURL + "/users")
URLProtocolMock.testURLs = [usersURL: Data(Fixtures.createUserResponse.utf8)]
//1) When is valid
APIDemo.publisher = customPublisher
URLProtocolMock.response = mocks.validResponse
let publisher = APIDemo.create(user: self.mocks.user)
let validTest = evalValidResponseTest(publisher: publisher)
wait(for: validTest.expectations, timeout: testTimeout)
validTest.cancellable?.cancel()
//2) When has invalid response
URLProtocolMock.response = mocks.invalidResponse
let publisher2 = APIDemo.create(user: self.mocks.user)
let invalidTest = evalInvalidResponseTest(publisher: publisher2)
wait(for: invalidTest.expectations, timeout: testTimeout)
invalidTest.cancellable?.cancel()
//3) When has invalid data and valid response
URLProtocolMock.testURLs[usersURL] = Data("{{}".utf8)
URLProtocolMock.response = mocks.validResponse
let publisher3 = APIDemo.create(user: self.mocks.user)
let invalidTest3 = evalInvalidResponseTest(publisher: publisher3)
wait(for: invalidTest3.expectations, timeout: testTimeout)
invalidTest3.cancellable?.cancel()
//4) Network Failure
URLProtocolMock.response = mocks.validResponse
URLProtocolMock.error = mocks.networkError
let publisher4 = APIDemo.create(user: self.mocks.user)
let invalidTest4 = evalInvalidResponseTest(publisher: publisher4)
wait(for: invalidTest4.expectations, timeout: testTimeout)
invalidTest4.cancellable?.cancel()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment