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"),
]
// ...
}