This is intended to be a tutorial to get you started using FitNesse with ruby on Rails. There is some explanation needed about FitNesse, Fit, and Rails. Smart people have written about such things, I will leave you to Google them as interested. Two good references are listed at the bottom of this tutorial.
So lets get started. Our customer has just written a test that looks like this:
This would be a common first fixture for a product, as it is limited in scope, it is a very specific example from which to build a conversation about requirements about. Anyway, the first thing to making this pass is getting some of the FitNesse infrastructure in place around your rails app.
First, lets create a rails app for our vending machine:
We see all the normal rails stuff, all the creates. Lets download FitNesse and place it in our vending_machine
root directory. Then start the FitNesse server.
So, now we go to the FitNesse main page and create the page for our acceptance test. Then put the test from the customer into the page and try to run it. Failures? We need to set up the environment to run with Ruby fit.
Follow the instructions to set up the right test runner for ruby. Lets write our first fixture in a fixtures directory we create in the root folder.
Our new test with the path and test runner set up looks like this:
There are a few things to notice about the changes:
-
The path is the rails root
dir
. -
Fixtures.VendingMachine
is mapping to the folder name/module name of where the fixtures are located. - If you run the test, you will get all sorts of exceptions, because the fixture code is not written yet.
Now it is time to write a fixture to make this test fail without any exceptions. In the fixtures directory, create a file called vending_machine.rb
.
The naming of your fixtures maps with the name of your file, which it will require. Lets make a stub file to make the test fail without exceptions.
Now we should have failures with the amount not changing. Lets create our controller for the vending machine and start to implement the code. I am going to leave out the specs I use to write the controller, just show the code.
Here is the first version of the controller. Note it has the same stubbed methods the fixture has:
Lets hook up the fixture to the imaginary controller. There is a fair amount of set up which is rails related. First, we need to load up the environment and the controllers. Add these lines to the beginning of your fixture:
Which should allow us to change the initialize method to:
Which creates the controller, and initializes its params to an empty dictionary. There are other ways to do this, like creating test params, but for simplicity, we are going to create our own for now.
Next, we set up the values in the params and call the controller methods:
Now, we make the test pass by implementing the controller:
Missing Requires
There is some weirdness around the way RubyFit collects exceptions after failing to find the file to require. If this occurs, dig into ruby fit’s FixtureLoader
, specifically the find_class
method. Usually you can get some information from the exceptions being silently caught.