FBSnapshotTestCase does not create the FB_REFERENCE_IMAGE_DIR and IMAGE_DIFF_DIR folders

1k views Asked by At

this is my test :

class ZabiTests: FBSnapshotTestCase {

var vc:UIViewController?
override func setUpWithError() throws {
    super.setUp()
    vc = ViewController()
   recordMode = true
}

override func tearDownWithError() throws {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testExample() throws {
  FBSnapshotVerifyView((vc?.view)!)
}

func testPerformanceExample() throws {
    // This is an example of a performance test case.
    self.measure {
        // Put the code you want to measure the time of here.
    }
}

}

thats the environment Variables :

first time i run with recordMode = true and then i run without this line, and still i get Snapshot comparison failed

(vc is okey, red background and not nil )

enter image description here

1

There are 1 answers

1
ShaLi Shaltiel On BEST ANSWER

After playing around with it I created an alternative way of auto defining those folders by creating my own base class:

open class MySnapshotTestCase: FBSnapshotTestCase {
  var referenceImageDirectory: String?
  var imageDiffDirectory: String?


  public func verify(
      view: UIView,
      file: StaticString = #file,
      line: UInt = #line
  ) {

    let directory = ("\(file)" as NSString).deletingLastPathComponent

    // Save for later use in getReferenceImageDirectory
    referenceImageDirectory = NSString.path(withComponents: [directory, "ReferenceImages"])

    // Save for later use in getImageDiffDirectory
    imageDiffDirectory = NSString.path(withComponents: [directory, "FailedSnapshotTests"])

    FBSnapshotVerifyView(
        view,
        identifier: nil,
        perPixelTolerance: 0,
        overallTolerance: 0,
        file: file,
        line: line)
  }

  open override func getReferenceImageDirectory(withDefault dir: String?) -> String {
    guard let referenceImageDirectory = referenceImageDirectory else {
      fatalError("Do not call FBSnapshotVerifyView directly, use WazeSnapshotTestCase.verify instead.")
    }
    return referenceImageDirectory
  }

  open override func getImageDiffDirectory(withDefault dir: Swift.String?) -> Swift.String {
    guard let imageDiffDirectory = imageDiffDirectory else {
      fatalError("Do not call FBSnapshotVerifyView directly, use WazeSnapshotTestCase.verify instead.")
    }
    
    return imageDiffDirectory
  }
}