Anonymous controller testing in Minitest
I believe binding.pry and Rspec respectively are the best things that could happen to Ruby world. I've been using both for the same time I've been using Ruby and I couldn't imagine using another testing library. Until recently, because I entered a project using Minitest.
My first task was relatively easy: I had to add before_filter that sets language based on the user's preference. In a very basic form it's just a matter of adding these lines to ApplicaionController:
In order to test this code, we should create an anonymous controller in the test and make sure it renders the template using proper language.
Rspec test for this would be extremely easy:
The only problem is - we are not using Rspec. I did not found such an elegant way to create an anonymous controller in Minitest, but it's possible to do define it with class_eval:
Unfortunately, this is not enough, because we also need to add a route to be able to call this from the test:
The disable_clear_and_finalize is necessary. If we don't call it, we'll overwrite all routes we already have with nothing, so following tests using any route won't work. Now, having all of these set up we can get to the actual test:
I'm happy with this test, but still not convinced to Minitest. I find Rspec DSL simply perfect for exploring every possible scenario of the tested subject, whether it's a small unit or a large piece of functionality. There's also a chance that I'm biased and ten years of using Rspec formed the way I perceive automated tests. I guess I'll review my opinion after few months of using Minitest.
Comments
Post a Comment