Test fixtures and behavioral testing
Yesterday, I was coding a new set of tests for a Picture class. I wanted to introduce an on-disk cache, when the picture is public.
I already had an existing test/unit/picture_test.rb, which I extended.
I knew this was the wrong way to go – the fixture was repeated more than once. Then, I realized that what I wanted was a new TestCase. I had already reached for my mouse to hit New File when I had a flash: what prevents me from simply adding a new class to picture_test.rb ?
I also had another flash: is TestCase the right name ? Shouldn’t it be called TestFixture instead ? Because that is what instances of this class represent – a test fixture.
This is very much akin to Spec::Context, from RSpec.
And you see the tests ? They all respect the one assertion per test. I was quite happy with the results.
Now, I need to refactor those other instances in my test suite…
Leave a Reply