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

Was this helpful?