Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
final class MovieManagerTests: XCTestCase {
private var storeSpy: KeyValueStoreSpy!
private var clientSpy: ClientSpy!
private var movieManager: MovieManaging!
override func setUp() {
super.setUp()
storeSpy = KeyValueStoreSpy()
clientSpy = ClientSpy()
movieManager = MovieManager(client: clientSpy, store: storeSpy)
}
override func tearDown() {
defer { super.tearDown() }
storeSpy = nil
clientSpy = nil
movieManager = nil
}
// MARK: - getPopularMovies(_:)
func test_getPopularMovies_should_get_a_list_of_movies_from_the_server() {
let expectedMovies = [Movie(...), Movie(...)]
clientSpy.getResultStub["/movies/popular"] = Result<[Movie], MovieManagerError>.success(expectedMovies)
let expectation = self.expectation(description: "get_movies")
movieManagerSpy.getPopularMovies { result in
switch result {
case .success(let movies):
XCTAssertEqual(movies, expectedMovies)
XCTAssertEqual(clientSpy.pathRecords.first, "/movies/popular")
XCTAssertEqual(clientSpy.pathRecords.count, 1)
case .failure(let error):
XCTFailure("Unexpected error: \(error)")
}
expectation.fulfill()
}
wait(for: [expectation], timeout: 1)
}
func test_getPopularMovies_should_cache_two_movies() {
let expectedMovies = [Movie(...), Movie(...)]
clientSpy.getResultStub["/movies/popular"] = Result<[Movie], MovieManagerError>.success(expectedMovies)
let expectation = self.expectation(description: "get_movies")
movieManagerSpy.getPopularMovies { result in
switch result {
case .success(let movies):
var storeSpyKeyRecords = storeSpy.keyRecords
XCTAssertEqual(storeSpyKeyRecords.removeFirst(), expectedMovies[0].id)
XCTAssertEqual(storeSpyKeyRecords.removeFirst(), expectedMovies[1].id)
XCTAssertEqual(storeSpy.keyRecords.count, 2)
XCTAssertEqual(movie, expectedMovie)
case .failure(let error):
XCTFailure("Unexpected error: \(error)")
}
expectation.fulfill()
}
wait(for: [expectation], timeout: 1)
}
// MARK: - getMovie(id:completion:)
func test_getMovie_should_get_a_movie_from_store() {
let expectedMovie = Movie(id: "42", ...)
storeSpy.getEntryResultStub["42"] = expectedMovie
let expectation = self.expectation(description: "get_movie")
movieManagerSpy.getMovie(id: "42") { result in
switch result {
case .success(let movie):
XCTAssertEqual(storeSpy.keyRecords.count, 1)
XCTAssertEqual(storeSpy.keyRecords.first, "42")
XCTAssertEqual(storeSpy.valueRecords.count, 0)
XCTAssertEqual(clientSpy.pathRecords.count, 0)
XCTAssertEqual(movie, expectedMovie)
case .failure(let error):
XCTFailure("Unexpected error: \(error)")
}
}
}
func test_getMovie_should_get_a_movie_from_server() {
let expectedMovie = Movie(id: "42", ...)
clientSpy.getResultStub["/movies/42"] = Result<Movie, MovieManagerError>.success(expectedMovie)
let expectation = self.expectation(description: "get_movie")
movieManagerSpy.getMovie(id: "42") { result in
switch result {
case .success(let movie):
XCTAssertEqual(clientSpy.pathRecords.first, "/movies/42")
XCTAssertEqual(clientSpy.pathRecords.count, 1)
XCTAssertEqual(movie, expectedMovie)
case .failure(let error):
XCTFailure("Unexpected error: \(error)")
}
}
}
func test_getMovie_should_get_a_movie_from_server_and_store_it() {
let expectedMovie = Movie(id: "42", ...)
clientSpy.getResultStub["/movies/42"] = Result<Movie, MovieManagerError>.success(expectedMovie)
let expectation = self.expectation(description: "get_movie")
movieManagerSpy.getMovie(id: "42") { result in
switch result {
case .success(let movie):
XCTAssertEqual(storeSpy.valueRecords.count, 1)
XCTAssertEqual(storeSpy.valueRecords.first as? Movie, expectedMovie)
XCTAssertEqual(storeSpy.keyRecords.count, 1)
XCTAssertEqual(storeSpy.keyRecords.first, "42")
case .failure(let error):
XCTFailure("Unexpected error: \(error)")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment