Fixtures

โ„น๏ธ How can you modify the type signatures of your production code without having to update the tests that consume them tooโ“

๐Ÿ…ฐ๏ธ By decoupling the tests from the production object initialization code with fixtures.

Define a fixture extension:

// MenuItem+fixture.swift 
@testable import Albertos 
extension MenuItem {
    // โญ๏ธ fixture
    static func fixture( 
        category: String = "category", 
        name    : String = "name" 
    ) -> MenuItem 
    {
        // โญ๏ธ production object initialization code
        MenuItem(category: category, name: name) 
    }
}

and update our test code from:

func testMenuItems() {
    let menu = [ 
        // โญ๏ธ production object initialization code
        MenuItem(category: "pastas", name: "name"), 
        MenuItem(category: "pastas", name: "other name"), 
    ]
    // ...
}

to:

func testMenuItems() {
    let menu = [ 
        // โญ๏ธ using fixtures (decoupling production init code)
        MenuItem.fixture(category: "pastas", name: "name"), 
        MenuItem.fixture(category: "pastas", name: "other name"), 
    ]
    // ...
}

Last updated