Skip to content

Commit

Permalink
Replace try! with throwing test methods
Browse files Browse the repository at this point in the history
`try!` can cause crashes, which stops the test execution. When possible, it's best to throw errors in test methods, so you get a failing test rather than a crash.
  • Loading branch information
caiozullo committed Mar 22, 2024
1 parent 0fbdd1a commit a119271
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ class EssentialFeedCacheIntegrationTests: XCTestCase {

// MARK: - LocalFeedLoader Tests

func test_loadFeed_deliversNoItemsOnEmptyCache() {
let feedLoader = makeFeedLoader()
func test_loadFeed_deliversNoItemsOnEmptyCache() throws {
let feedLoader = try makeFeedLoader()

expect(feedLoader, toLoad: [])
}

func test_loadFeed_deliversItemsSavedOnASeparateInstance() {
let feedLoaderToPerformSave = makeFeedLoader()
let feedLoaderToPerformLoad = makeFeedLoader()
func test_loadFeed_deliversItemsSavedOnASeparateInstance() throws {
let feedLoaderToPerformSave = try makeFeedLoader()
let feedLoaderToPerformLoad = try makeFeedLoader()
let feed = uniqueImageFeed().models

save(feed, with: feedLoaderToPerformSave)

expect(feedLoaderToPerformLoad, toLoad: feed)
}

func test_saveFeed_overridesItemsSavedOnASeparateInstance() {
let feedLoaderToPerformFirstSave = makeFeedLoader()
let feedLoaderToPerformLastSave = makeFeedLoader()
let feedLoaderToPerformLoad = makeFeedLoader()
func test_saveFeed_overridesItemsSavedOnASeparateInstance() throws {
let feedLoaderToPerformFirstSave = try makeFeedLoader()
let feedLoaderToPerformLastSave = try makeFeedLoader()
let feedLoaderToPerformLoad = try makeFeedLoader()
let firstFeed = uniqueImageFeed().models
let latestFeed = uniqueImageFeed().models

Expand All @@ -50,9 +50,9 @@ class EssentialFeedCacheIntegrationTests: XCTestCase {
expect(feedLoaderToPerformLoad, toLoad: latestFeed)
}

func test_validateFeedCache_doesNotDeleteRecentlySavedFeed() {
let feedLoaderToPerformSave = makeFeedLoader()
let feedLoaderToPerformValidation = makeFeedLoader()
func test_validateFeedCache_doesNotDeleteRecentlySavedFeed() throws {
let feedLoaderToPerformSave = try makeFeedLoader()
let feedLoaderToPerformValidation = try makeFeedLoader()
let feed = uniqueImageFeed().models

save(feed, with: feedLoaderToPerformSave)
Expand All @@ -61,9 +61,9 @@ class EssentialFeedCacheIntegrationTests: XCTestCase {
expect(feedLoaderToPerformSave, toLoad: feed)
}

func test_validateFeedCache_deletesFeedSavedInADistantPast() {
let feedLoaderToPerformSave = makeFeedLoader(currentDate: .distantPast)
let feedLoaderToPerformValidation = makeFeedLoader(currentDate: Date())
func test_validateFeedCache_deletesFeedSavedInADistantPast() throws {
let feedLoaderToPerformSave = try makeFeedLoader(currentDate: .distantPast)
let feedLoaderToPerformValidation = try makeFeedLoader(currentDate: Date())
let feed = uniqueImageFeed().models

save(feed, with: feedLoaderToPerformSave)
Expand All @@ -74,10 +74,10 @@ class EssentialFeedCacheIntegrationTests: XCTestCase {

// MARK: - LocalFeedImageDataLoader Tests

func test_loadImageData_deliversSavedDataOnASeparateInstance() {
let imageLoaderToPerformSave = makeImageLoader()
let imageLoaderToPerformLoad = makeImageLoader()
let feedLoader = makeFeedLoader()
func test_loadImageData_deliversSavedDataOnASeparateInstance() throws {
let imageLoaderToPerformSave = try makeImageLoader()
let imageLoaderToPerformLoad = try makeImageLoader()
let feedLoader = try makeFeedLoader()
let image = uniqueImage()
let dataToSave = anyData()

Expand All @@ -87,11 +87,11 @@ class EssentialFeedCacheIntegrationTests: XCTestCase {
expect(imageLoaderToPerformLoad, toLoad: dataToSave, for: image.url)
}

func test_saveImageData_overridesSavedImageDataOnASeparateInstance() {
let imageLoaderToPerformFirstSave = makeImageLoader()
let imageLoaderToPerformLastSave = makeImageLoader()
let imageLoaderToPerformLoad = makeImageLoader()
let feedLoader = makeFeedLoader()
func test_saveImageData_overridesSavedImageDataOnASeparateInstance() throws {
let imageLoaderToPerformFirstSave = try makeImageLoader()
let imageLoaderToPerformLastSave = try makeImageLoader()
let imageLoaderToPerformLoad = try makeImageLoader()
let feedLoader = try makeFeedLoader()
let image = uniqueImage()
let firstImageData = Data("first".utf8)
let lastImageData = Data("last".utf8)
Expand All @@ -105,18 +105,18 @@ class EssentialFeedCacheIntegrationTests: XCTestCase {

// MARK: - Helpers

private func makeFeedLoader(currentDate: Date = Date(), file: StaticString = #filePath, line: UInt = #line) -> LocalFeedLoader {
private func makeFeedLoader(currentDate: Date = Date(), file: StaticString = #filePath, line: UInt = #line) throws -> LocalFeedLoader {
let storeURL = testSpecificStoreURL()
let store = try! CoreDataFeedStore(storeURL: storeURL)
let store = try CoreDataFeedStore(storeURL: storeURL)
let sut = LocalFeedLoader(store: store, currentDate: { currentDate })
trackForMemoryLeaks(store, file: file, line: line)
trackForMemoryLeaks(sut, file: file, line: line)
return sut
}

private func makeImageLoader(file: StaticString = #filePath, line: UInt = #line) -> LocalFeedImageDataLoader {
private func makeImageLoader(file: StaticString = #filePath, line: UInt = #line) throws -> LocalFeedImageDataLoader {
let storeURL = testSpecificStoreURL()
let store = try! CoreDataFeedStore(storeURL: storeURL)
let store = try CoreDataFeedStore(storeURL: storeURL)
let sut = LocalFeedImageDataLoader(store: store)
trackForMemoryLeaks(store, file: file, line: line)
trackForMemoryLeaks(sut, file: file, line: line)
Expand Down Expand Up @@ -178,11 +178,7 @@ class EssentialFeedCacheIntegrationTests: XCTestCase {
}

private func testSpecificStoreURL() -> URL {
return cachesDirectory().appendingPathComponent("\(type(of: self)).store")
}

private func cachesDirectory() -> URL {
return FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
.cachesDirectory.appendingPathComponent("\(type(of: self)).store")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import EssentialFeed

class CoreDataFeedImageDataStoreTests: XCTestCase {

func test_retrieveImageData_deliversNotFoundWhenEmpty() {
let sut = makeSUT()
func test_retrieveImageData_deliversNotFoundWhenEmpty() throws {
let sut = try makeSUT()

expect(sut, toCompleteRetrievalWith: notFound(), for: anyURL())
}

func test_retrieveImageData_deliversNotFoundWhenStoredDataURLDoesNotMatch() {
let sut = makeSUT()
func test_retrieveImageData_deliversNotFoundWhenStoredDataURLDoesNotMatch() throws {
let sut = try makeSUT()
let url = URL(string: "http://a-url.com")!
let nonMatchingURL = URL(string: "http://another-url.com")!

Expand All @@ -23,8 +23,8 @@ class CoreDataFeedImageDataStoreTests: XCTestCase {
expect(sut, toCompleteRetrievalWith: notFound(), for: nonMatchingURL)
}

func test_retrieveImageData_deliversFoundDataWhenThereIsAStoredImageDataMatchingURL() {
let sut = makeSUT()
func test_retrieveImageData_deliversFoundDataWhenThereIsAStoredImageDataMatchingURL() throws {
let sut = try makeSUT()
let storedData = anyData()
let matchingURL = URL(string: "http://a-url.com")!

Expand All @@ -33,8 +33,8 @@ class CoreDataFeedImageDataStoreTests: XCTestCase {
expect(sut, toCompleteRetrievalWith: found(storedData), for: matchingURL)
}

func test_retrieveImageData_deliversLastInsertedValue() {
let sut = makeSUT()
func test_retrieveImageData_deliversLastInsertedValue() throws {
let sut = try makeSUT()
let firstStoredData = Data("first".utf8)
let lastStoredData = Data("last".utf8)
let url = URL(string: "http://a-url.com")!
Expand All @@ -47,9 +47,9 @@ class CoreDataFeedImageDataStoreTests: XCTestCase {

// - MARK: Helpers

private func makeSUT(file: StaticString = #filePath, line: UInt = #line) -> CoreDataFeedStore {
private func makeSUT(file: StaticString = #filePath, line: UInt = #line) throws -> CoreDataFeedStore {
let storeURL = URL(fileURLWithPath: "/dev/null")
let sut = try! CoreDataFeedStore(storeURL: storeURL)
let sut = try CoreDataFeedStore(storeURL: storeURL)
trackForMemoryLeaks(sut, file: file, line: line)
return sut
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,77 +7,77 @@ import EssentialFeed

class CoreDataFeedStoreTests: XCTestCase, FeedStoreSpecs {

func test_retrieve_deliversEmptyOnEmptyCache() {
let sut = makeSUT()
func test_retrieve_deliversEmptyOnEmptyCache() throws {
let sut = try makeSUT()

assertThatRetrieveDeliversEmptyOnEmptyCache(on: sut)
}

func test_retrieve_hasNoSideEffectsOnEmptyCache() {
let sut = makeSUT()
func test_retrieve_hasNoSideEffectsOnEmptyCache() throws {
let sut = try makeSUT()

assertThatRetrieveHasNoSideEffectsOnEmptyCache(on: sut)
}

func test_retrieve_deliversFoundValuesOnNonEmptyCache() {
let sut = makeSUT()
func test_retrieve_deliversFoundValuesOnNonEmptyCache() throws {
let sut = try makeSUT()

assertThatRetrieveDeliversFoundValuesOnNonEmptyCache(on: sut)
}

func test_retrieve_hasNoSideEffectsOnNonEmptyCache() {
let sut = makeSUT()
func test_retrieve_hasNoSideEffectsOnNonEmptyCache() throws {
let sut = try makeSUT()

assertThatRetrieveHasNoSideEffectsOnNonEmptyCache(on: sut)
}

func test_insert_deliversNoErrorOnEmptyCache() {
let sut = makeSUT()
func test_insert_deliversNoErrorOnEmptyCache() throws {
let sut = try makeSUT()

assertThatInsertDeliversNoErrorOnEmptyCache(on: sut)
}

func test_insert_deliversNoErrorOnNonEmptyCache() {
let sut = makeSUT()
func test_insert_deliversNoErrorOnNonEmptyCache() throws {
let sut = try makeSUT()

assertThatInsertDeliversNoErrorOnNonEmptyCache(on: sut)
}

func test_insert_overridesPreviouslyInsertedCacheValues() {
let sut = makeSUT()
func test_insert_overridesPreviouslyInsertedCacheValues() throws {
let sut = try makeSUT()

assertThatInsertOverridesPreviouslyInsertedCacheValues(on: sut)
}

func test_delete_deliversNoErrorOnEmptyCache() {
let sut = makeSUT()
func test_delete_deliversNoErrorOnEmptyCache() throws {
let sut = try makeSUT()

assertThatDeleteDeliversNoErrorOnEmptyCache(on: sut)
}

func test_delete_hasNoSideEffectsOnEmptyCache() {
let sut = makeSUT()
func test_delete_hasNoSideEffectsOnEmptyCache() throws {
let sut = try makeSUT()

assertThatDeleteHasNoSideEffectsOnEmptyCache(on: sut)
}

func test_delete_deliversNoErrorOnNonEmptyCache() {
let sut = makeSUT()
func test_delete_deliversNoErrorOnNonEmptyCache() throws {
let sut = try makeSUT()

assertThatDeleteDeliversNoErrorOnNonEmptyCache(on: sut)
}

func test_delete_emptiesPreviouslyInsertedCache() {
let sut = makeSUT()
func test_delete_emptiesPreviouslyInsertedCache() throws {
let sut = try makeSUT()

assertThatDeleteEmptiesPreviouslyInsertedCache(on: sut)
}

// - MARK: Helpers

private func makeSUT(file: StaticString = #filePath, line: UInt = #line) -> FeedStore {
private func makeSUT(file: StaticString = #filePath, line: UInt = #line) throws -> FeedStore {
let storeURL = URL(fileURLWithPath: "/dev/null")
let sut = try! CoreDataFeedStore(storeURL: storeURL)
let sut = try CoreDataFeedStore(storeURL: storeURL)
trackForMemoryLeaks(sut, file: file, line: line)
return sut
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@
import Foundation

protocol FeedStoreSpecs {
func test_retrieve_deliversEmptyOnEmptyCache()
func test_retrieve_hasNoSideEffectsOnEmptyCache()
func test_retrieve_deliversFoundValuesOnNonEmptyCache()
func test_retrieve_hasNoSideEffectsOnNonEmptyCache()
func test_retrieve_deliversEmptyOnEmptyCache() throws
func test_retrieve_hasNoSideEffectsOnEmptyCache() throws
func test_retrieve_deliversFoundValuesOnNonEmptyCache() throws
func test_retrieve_hasNoSideEffectsOnNonEmptyCache() throws

func test_insert_deliversNoErrorOnEmptyCache()
func test_insert_deliversNoErrorOnNonEmptyCache()
func test_insert_overridesPreviouslyInsertedCacheValues()
func test_insert_deliversNoErrorOnEmptyCache() throws
func test_insert_deliversNoErrorOnNonEmptyCache() throws
func test_insert_overridesPreviouslyInsertedCacheValues() throws

func test_delete_deliversNoErrorOnEmptyCache()
func test_delete_hasNoSideEffectsOnEmptyCache()
func test_delete_deliversNoErrorOnNonEmptyCache()
func test_delete_emptiesPreviouslyInsertedCache()
func test_delete_deliversNoErrorOnEmptyCache() throws
func test_delete_hasNoSideEffectsOnEmptyCache() throws
func test_delete_deliversNoErrorOnNonEmptyCache() throws
func test_delete_emptiesPreviouslyInsertedCache() throws
}

protocol FailableRetrieveFeedStoreSpecs: FeedStoreSpecs {
func test_retrieve_deliversFailureOnRetrievalError()
func test_retrieve_hasNoSideEffectsOnFailure()
func test_retrieve_deliversFailureOnRetrievalError() throws
func test_retrieve_hasNoSideEffectsOnFailure() throws
}

protocol FailableInsertFeedStoreSpecs: FeedStoreSpecs {
func test_insert_deliversErrorOnInsertionError()
func test_insert_hasNoSideEffectsOnInsertionError()
func test_insert_deliversErrorOnInsertionError() throws
func test_insert_hasNoSideEffectsOnInsertionError() throws
}

protocol FailableDeleteFeedStoreSpecs: FeedStoreSpecs {
func test_delete_deliversErrorOnDeletionError()
func test_delete_hasNoSideEffectsOnDeletionError()
func test_delete_deliversErrorOnDeletionError() throws
func test_delete_hasNoSideEffectsOnDeletionError() throws
}

typealias FailableFeedStoreSpecs = FailableRetrieveFeedStoreSpecs & FailableInsertFeedStoreSpecs & FailableDeleteFeedStoreSpecs

0 comments on commit a119271

Please sign in to comment.