From a1192714def341d637c45469e60ae14112c1b3c2 Mon Sep 17 00:00:00 2001 From: Caio Zullo Date: Fri, 22 Mar 2024 11:16:41 +0200 Subject: [PATCH] Replace `try!` with throwing test methods `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. --- .../EssentialFeedCacheIntegrationTests.swift | 62 +++++++++---------- .../CoreDataFeedImageDataStoreTests.swift | 20 +++--- .../Feed Cache/CoreDataFeedStoreTests.swift | 48 +++++++------- .../FeedStoreSpecs/FeedStoreSpecs.swift | 34 +++++----- 4 files changed, 80 insertions(+), 84 deletions(-) diff --git a/EssentialFeed/EssentialFeedCacheIntegrationTests/EssentialFeedCacheIntegrationTests.swift b/EssentialFeed/EssentialFeedCacheIntegrationTests/EssentialFeedCacheIntegrationTests.swift index 2128f702..79cd64d4 100644 --- a/EssentialFeed/EssentialFeedCacheIntegrationTests/EssentialFeedCacheIntegrationTests.swift +++ b/EssentialFeed/EssentialFeedCacheIntegrationTests/EssentialFeedCacheIntegrationTests.swift @@ -21,15 +21,15 @@ 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) @@ -37,10 +37,10 @@ class EssentialFeedCacheIntegrationTests: XCTestCase { 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 @@ -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) @@ -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) @@ -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() @@ -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) @@ -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) @@ -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") } } diff --git a/EssentialFeed/EssentialFeedTests/Feed Cache/CoreDataFeedImageDataStoreTests.swift b/EssentialFeed/EssentialFeedTests/Feed Cache/CoreDataFeedImageDataStoreTests.swift index f37c8782..6f1f1194 100644 --- a/EssentialFeed/EssentialFeedTests/Feed Cache/CoreDataFeedImageDataStoreTests.swift +++ b/EssentialFeed/EssentialFeedTests/Feed Cache/CoreDataFeedImageDataStoreTests.swift @@ -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")! @@ -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")! @@ -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")! @@ -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 } diff --git a/EssentialFeed/EssentialFeedTests/Feed Cache/CoreDataFeedStoreTests.swift b/EssentialFeed/EssentialFeedTests/Feed Cache/CoreDataFeedStoreTests.swift index f858b7aa..f048a963 100644 --- a/EssentialFeed/EssentialFeedTests/Feed Cache/CoreDataFeedStoreTests.swift +++ b/EssentialFeed/EssentialFeedTests/Feed Cache/CoreDataFeedStoreTests.swift @@ -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 } diff --git a/EssentialFeed/EssentialFeedTests/Feed Cache/FeedStoreSpecs/FeedStoreSpecs.swift b/EssentialFeed/EssentialFeedTests/Feed Cache/FeedStoreSpecs/FeedStoreSpecs.swift index 548954bf..29c2650f 100644 --- a/EssentialFeed/EssentialFeedTests/Feed Cache/FeedStoreSpecs/FeedStoreSpecs.swift +++ b/EssentialFeed/EssentialFeedTests/Feed Cache/FeedStoreSpecs/FeedStoreSpecs.swift @@ -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