In this blog thoughtbot team outlined how they test their factories first. I like this approach. Since we prefer using minitest here is how we implemented it. It is similar to how the thoughtbot blog has described. However I still want to blog about it so that in our other projects we can use similar approach.
First under spec directory create a file called factories_spec.rb . Here is how our file looks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Next I need to tell rake to always run this test file first.
When rake command is executed then it goes through all the .rake and loads them. So all we need to do is to create a rake file called factory.rake and put this file under lib/tasks .
1 2 3 4 5 6 | |
Here a dependecy is being added to test . And if factory test fails then dependency is not met and the main test suite will not run.
That’s it. Now each unit test does not need to test factory first. All factories are getting tested here.