swiftunit-testingxctestquick-nimble

Nimble matching of struct via "identical to"


I am using nimble in unit test expectation mapping and having a question about comparing structs.

What i am observing is that the matched .to(be(x)) does not work at all with structs. So this following unit test fails:

func someTest() {
    struct Struct {
        let a: String
        let b: String
    }
    let structure = Struct(a: "a", b: "b")
    expect(structure).to(be(structure))
}

Does this mean that the copy on write mechanism kicks in here and we are looking on 2 copies? Why is that test failing?


Solution

  • The be() function actually calls beIdenticalTo, which uses pointer equality checking, so it only works for reference types. See the BeIdenticalTo source code.

    You should make Struct conform to Equatable and use equal instead.

    func someTest() {
        struct Struct: Equatable {
            let a: String
            let b: String
        }
        let structure = Struct(a: "a", b: "b")
        expect(structure).to(equal(structure))
    }