We know that we want our tests to be repeatable, simple and fast. Fast is the key word here. Some tests involve tasks that we don’t want to be run because they involve high time consumption. A classic example is trying to avoid the connection to the database when is not neccessary. This testing approach has lead to a whole framework called UnitRecord that allows us to test ActiveRecord models without hitting the database. This nicely wrapped gem makes use of stubs for faking database columns. But no so fast; let’s explain before what are stubs and mocks.

First of all should we point the difference (huge difference) between mocking and stubbing, as some times is not well understood. Stubs and mocks are similar concepts that drive to completely different testing philosophies, the classic TDD (Test Driven Development) and the fairly new BDD (Behaviour Driven Development).

On one hand, we would want to test our classes and methods by the results they give to us. If we have a method that takes an array of integers and returns it in ascendant order, we should consider writing a test which will take the array [4,3,5] and check that the result will be [3,4,5]. We don’t care about how our software do that, we care about whether what our software is giving as a result. This is classic TDD.On the other hand, we know that there are different algorithms for sorting an array. Maybe we were using an easy insertion sort algorithm, then we realised that the sets of numbers were getting bigger and bigger and decided to switch to a quick sort. The method is the same, the result as well, the way it does it is different. Testing our software paying attention at how it does it (and thus giving to the test cases our expectations on what the software is going to do in order to solve the task) is BDD.

A still-awake reader would have noticed that BBD involves highly coupled tests. A mock will have our expectations on the behaviour of the method declared, and our expectations will change every time we change the internal algorithm of our method. This is, I think, the biggest drawback of BDD and mocks. It has many advantages though, especially about the need to think about the design of our software from the very begging, when we are writing our test (yes, before start coding the real app), if you name correctly your tests cases, of course. BDD promotes also some good design techniques and autocomments the implementation code on the test (as we are describing there how our code works). Martin Fowler has an excellent article comparing advantages and disadvantages of both approaches, with some examples written in Java. And if you decide to go BDD, you should check RSpec for Ruby.If you still have to decide, we have in Ruby a marvellous tool that allows us to stub and mock with one single framework, called Mocha. RoR developers coming from Java will be happy as the syntax of Ruby Mocha is intended to be as close as possible to JMock. Here it is a brief test example of mocking:

def test_should_start_engine

And an example of stubbing with Mocha:

def test_should_return_false_for_failed_print
	document = stub("my document")
	ui = View.new(document)
	assert_equal false, ui.print

Notice how in the first case we declare our expectations, while in the second case the assert the result of the test (this phase is called verification). We don’t have verification phase in BDD, thus we don’t have assertions.

Further reading

NetBeans 6.0 Ruby Edition

November 20, 2007

For those who definitely don’t like the acclaimed TextMate when developing Ruby on Rails applications, chances were not very wide so far. We had RadRails, a RoR focused IDE, who showed off months ago as a promising working frame in its earliest versions, but has been lately reported that its developers have start loosing interest in improving it. We had also Eclipse, whose Ruby support has been also criticized.The finally we have since September the Ruby edition of the well-known NetBeans IDE. We have just installed it, so expect in the next day a comprehensive review. So far, it looks very promising. Let’s take a look at some of its advantages:

  • Only 19Mb to download, a fair memory consumption and a lot of helpful functionality out of the box.
  • It ships with code completion that works, extremely useful for those who never manage to learn by heart the rails framework. But also includes inline documentation just pressing CTRL + SPACE when typing a keyword.
  • Rake support and generations of models, scaffolds, controllers… just in one click!
  • Subversion integration to make your version control easier.
  • Of course, a lot of helpful syntax highlighting and helpers.
  • Built-in gem management!
  • Run test on the IDE and RSpec support.

Here you can find a great review of the NetBeans IDE, from where I have stealed the screenshots. Also you have the screencasts of the official NetBeans website.




Get every new post delivered to your Inbox.