Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Save failed snapshot if it differs from reference image #47

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
2.3.5
2.4.3

8 changes: 8 additions & 0 deletions Sources/SnapshotCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extension SnapshotCoordinator : SnapshotCoordinating {
let referenceImage = try fileManager.referenceImage(filename: filename, className: className)

guard snapshot.compare(withImage: referenceImage) else {
try fileManager.save(referenceImage: snapshot, filename: filename.failed, className: className)
throw SnapshotError.imageMismatch(filename: filename)
}
}
Expand All @@ -65,3 +66,10 @@ extension SnapshotCoordinator : SnapshotCoordinating {
return try fileManager.save(referenceImage: referenceImage, filename: filename, className: className)
}
}

fileprivate extension String {

var failed: String {
return "\(self)_failed"
}
}
1 change: 1 addition & 0 deletions Sources/SnapshotFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class DataHandler : DataHandling {
}

protocol SnapshotFileManaging {
@discardableResult
func save(referenceImage: UIImage, filename: String, className: String) throws -> URL
func referenceImage(filename: String, className: String) throws -> UIImage
}
Expand Down
16 changes: 16 additions & 0 deletions Tests/SnapshotTestTests/SnapshotCoordinatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ class SnapshotCoordinatorTests: XCTestCase {
XCTAssertEqual(error as? SnapshotError, SnapshotError.imageMismatch(filename: "redSquare"))
}
}

func testCompareSnapshot_withViewNotEqualToReferenceImage_shouldInvokeSaveWithFailedSnapshotAndFailedFilenameOnFileManager() {

// Given
let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
view.backgroundColor = .blue
fileManagerMock.referenceImageReturnValue = UIImage(testFilename: "redSquare", ofType: "png")

// When
XCTAssertThrowsError(try sut.compareSnapshot(of: view, options: [], functionName: "redSquare", line: 0)) { error in
XCTAssertEqual(fileManagerMock.saveInvokeCount, 1)
XCTAssertNotNil(fileManagerMock.saveReferenceImageArgument)
XCTAssertEqual(fileManagerMock.saveFilenameArgument, "redSquare_failed")
XCTAssertEqual(fileManagerMock.saveClassNameArgument, "CustomButtonTests")
}
}

func testCompareSnapshot_shouldInvokeReferenceImageWithCorrectFilenameAndClassNameOnFileManager() {

Expand Down