mardi 21 février 2017

How should I test "isEqual" method of value object types in BDD?

I'm new to BDD, even to whole testing world.

I'm trying to take BDD practice when writing a simple linear algebra library in swift. So there would be many value object types like Matrix, Vector etc. When writing code, I suppose I still need to stick to the TDD principle (am I right?):

Not writing any single line of code without a failing test

To implement a value object type, I need to make it conform to Equatable protocol and implement its == operator. This is adding code, so I need a failing test. How to write spec for this kinda scenario ?

One may suggest some approach like:

describe("Matrix") {
    it("should be value object") {
        let aMatrix = Matrix<Double>(rows: 3, cols:2)
        let sameMatrix = Matrix<Double>(rows: 3, cols:2)
        expect(sameMatrix) == aMatrix
        let differentMatrix = Matrix<Double>(rows: 4, cols: 2)
        expect(differentMatrix) != aMatrix
    }
}

This would be an ugly boilerplate for two reason:

  1. There may be plenty of value objects and I need to repeat it for all of them
  2. There may be plenty of cases that would cause two objects not equal. Taking the spec above for example, an implementation of == like return lhs.rows == rhs.rows would pass the test. In order to reveal this "bug", I need to add another expectation like expect(matrixWithDifferentColmunCount) != aMatrix. And again, this kinda repetition happens for all value object types

So, how should I test this "isEqual" ( or operator== ) method elegantly ? or shouldn't I test it at all ?


I'm using swift and Quick for testing framework. Quick provides a mechanism called SharedExample to reduce boilerplate. But since swift is a static typing language and Quick's shared example doesn't support generics, it can't be directly used to test value objects.

I came up with a workaround but don't consider it as an elegant one.

Aucun commentaire:

Enregistrer un commentaire