# Fixtures

{% hint style="info" %}
:information\_source: How can you <mark style="color:red;">**modify**</mark> the <mark style="color:red;">**type signatures**</mark> of your <mark style="color:orange;">**production code**</mark> <mark style="color:red;">**without**</mark> having to <mark style="color:red;">**update the tests**</mark> that consume them too:question:

:a: By <mark style="color:red;">**decoupling the tests**</mark> from the <mark style="color:orange;">**production object initialization code**</mark> with <mark style="color:red;">**fixtures**</mark>.
{% endhint %}

{% tabs %}
{% tab title="💈範例" %}
Define a <mark style="color:red;">**fixture extension**</mark>:

```swift
// 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 <mark style="color:red;">**test code**</mark> from:

```swift
func testMenuItems() {
    let menu = [ 
        // ⭐️ production object initialization code
        MenuItem(category: "pastas", name: "name"), 
        MenuItem(category: "pastas", name: "other name"), 
    ]
    // ...
}
```

to:

```swift
func testMenuItems() {
    let menu = [ 
        // ⭐️ using fixtures (decoupling production init code)
        MenuItem.fixture(category: "pastas", name: "name"), 
        MenuItem.fixture(category: "pastas", name: "other name"), 
    ]
    // ...
}
```
{% endtab %}

{% tab title="📗 參考" %}
* Test-Driven Development in Swift (2021), p.69
{% endtab %}
{% endtabs %}
