Method stub with MiniTest

  2012-05-24


MiniTest is a testing tool that provides the necessary resources to make your test suite a complete stack, supporting TDD, BDD, mocking and benchmarking. It comes by default with Ruby 1.9.x and is also available for Ruby 1.8.7.

It provides two styles of testing: Unit tests and Specs. I personally prefer Specs since they are easier to read. Find out more about this on the documentation site. If you’re not yet using it, I suggest you to give it a try. I have used it for writing unit, functional and integration tests and the results are amazing.

But that’s enough for the introduction of MiniTest since this is more to talk about how to stub methods using this tool.

So we basically have two options. The first approach is to do something like Aaron Patterson (a.k.a. @tenderlove) does on his PeepCode’s screencast. It’s something like this:

klass = Class.new User do
  define_method(:confirmed?) { true }
end
user = klass.new
user.confirmed?.must_equal true

What he basically is doing here is redifining the User class and assigning that redefinition to the variable klass. Within the definition block, he is also redifininig the confirmed? instance method for that class, so that when it is called, it returns whatever it is inside the block given to the define_method method, which in this case is a true value.

The second way to do this, which is cleaner, niftier, fancier and less complex is by using the MiniTest’s stub method:

User.stub :confirmed?, true do
  user = User.first
  user.confirmed?.must_equal true
end

Clear, precise and concise. This magic was not available until version 3.0.0 when they added support to this on this commit. Once again MiniTest impresses me by its incredible super cow powers.

comments powered by Disqus