Skip to content

Instantly share code, notes, and snippets.

@kidap
Last active October 7, 2019 16:23
Show Gist options
  • Select an option

  • Save kidap/b19bcdebe14ae63527a3f080a90fbf47 to your computer and use it in GitHub Desktop.

Select an option

Save kidap/b19bcdebe14ae63527a3f080a90fbf47 to your computer and use it in GitHub Desktop.
Creating test data

Type we want to test

struct Player {
    var name: String
    var age: String
    var address: String
    var wins: Int
    var losses: Int

    var hasWinningRecord: Bool {
        return wins > losses
    }
}

Create a helper function to wrap the initializer of the type above. Set a default value for all of the properties.

func createPlayer(
    name: String = "John",
    age: String = "Snow",
    address: String = "Beyond the Wall",
    wins: Int = 10,
    losses: Int = 0
    ) -> Player {

    return Player(name: name, age: age, address: address, wins: wins, losses: losses)
}

Use the helper function to create your test data. Since there are already default values, we only need to set the properties that we care about in the specific test we're working on.


// Test #1 - Is name set?
let player1 = createPlayer(name: "Karlo")
pass(player1.name == "Karlo")



// Test #2 - hasWinningRecord is set correctly

// #2a
let player2 = createPlayer(wins: 1, losses: 0)
pass(player2.hasWinningRecord == true)

// #2b
let player3 = createPlayer(wins: 0, losses: 1)
pass(player3.hasWinningRecord == false)

// #2c
let player4 = createPlayer(wins: 1, losses: 1)
pass(player4.hasWinningRecord == false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment