Upgrade to Pro — share decks privately, control downloads, hide ads and more …

FBSnapshotTestCase

 FBSnapshotTestCase

Presented @ Swift Meetup Hamburg

Sample code: https://github.com/pietbrauer/Beverages

Piet Brauer

July 09, 2015
Tweet

More Decks by Piet Brauer

Other Decks in Programming

Transcript

  1. We have ... • a simple UITableViewController setup • a

    Beverage model • a BeverageCell cell that displays data
  2. We want to ... • test cell interactions • setting

    texts on labels • interacting with the stepper • view layout code
  3. Model tests func testBeer() { let subject = Beverage.Beer XCTAssertEqual(subject.amount,

    0.5) XCTAssertEqual(subject.description, "Beer (0.5 l)") XCTAssertEqual(subject.emoji, "!") }
  4. Model logic tests func testSettingAmountDrunk() { var subject = Beverage.Tea

    // reset units drunk subject.unitsDrunk = 0 XCTAssertEqual(subject.unitsDrunk, 0) // set new value subject.unitsDrunk = 5 XCTAssertEqual(subject.unitsDrunk, 5) // Take other instance of tea and check if same amount applies let secondSubject = Beverage.Tea XCTAssertEqual(subject.unitsDrunk, 5) }
  5. Cell View tests func testCellConfiguration() { var beverage = Beverage.Beer

    beverage.unitsDrunk = 3 let subject = BeverageCell(style: .Default, reuseIdentifier: nil) subject.configure(beverage) XCTAssertEqual(subject.beverageEmojiLabel.text!, "!") XCTAssertEqual(subject.beverageNameLabel.text!, "Beer (0.5 l)") XCTAssertEqual(subject.amountDrunkLabel.text!, "3 drunk today") XCTAssertEqual(subject.stepper.value, 3) }
  6. Cell Logic tests func testStepper() { var beverage = Beverage.Beers

    beverage.unitsDrunk = 0 let subject = BeverageCell(style: .Default, reuseIdentifier: nil) subject.configure(beverage) // Set stepper value subject.stepper.value = 5 subject.stepperStepped(subject.stepper) // Expect that the amount drunk label has the right value XCTAssertEqual(subject.amountDrunkLabel.text!, "5 drunk today") }
  7. Usage 1. import FBSnapshotTestCase 2. Inherit from FBSnapshotTestCase 3. In

    setUp() set recordMode = true 4. Run unit tests 5. Remove setUp()