Different unit testing styles @ carwow
At carwow we still can’t agree on a unit-testing style to follow. During a code review I found a few test that were hard to understand. Carpe diem! I’ll rewrite the tests to follow my amazing testing style and all my colleagues will bow to the perfection of my code! No more fighting. Only the one true way!
The tests in questions where for the validation rules of a simple rails models.
The model is called DealershipGroupRelation and it’s just the connection in a many-to-many relationships between the table Dealership and DealershipGroup.
The model is very simple, we just care about testing that the validations are in place:
I’ve paired with Akash Bhalla on rewriting the specs in a format the would be more understandable. Akash and I have started unit testing long time ago, way before rspec and way before other BDD frameworks. During our formative years we have been exposed to concept like:
Duplication in unit tests is fine: http://blog.jayfields.com/2008/05/testing-duplicate-code-in-your-tests.html
More on google about DRY and Unit testing: https://www.google.co.uk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=dry+and+unit+tests
4 phase test: https://robots.thoughtbot.com/four-phase-test
And Akash’s own article: http://www.akashb.com/blog/2014/06/25/is-duplication-always-a-bad-thing/
So the tests below could be shortened dramatically using all the fancy rspec functionalities like let, subject, etc. but Akash and I prefer not to use them. I will occasionally use let… but I’m not that fussed. I can do most of it with simple methods when I need to.
That’s it. That’s the way I do it. But let’s try a completely different approach. Other people at carwow embraced rspec fully even with his long experience of testing in less flexible languages like Java and C++.
The above test is well written and follows RSpec recommendations. It’s testing the right things. I personally find it’s overusing context, and in general I hate the advanced RSpec syntax because it looks very different from everyday code.
rspec is a testing DSL. minitest is ruby.
-- Adam Hawkins, "Bow Before MiniTest"
I can see why DHH says that:
Let’s see how the above test will look in the plain unit test style of Minitest
It’s not bad. There is beauty in using the simple syntax of a class where each method is a test. I miss the time before RSpec, but maybe it’s just nostalgia. I’m sure there were very good reasons why I’ve started using it… weren’t they?
Interested in making an Impact? Join the carwow-team!
Feeling social? Connect with us on Twitter and LinkedIn :-)