Part 1: General Installation and Using with a new Rails Project
We are going to install rspec for our testing framework and autotest is going to be the actual thing that runs all your tests.
How, autotest works is it watches for when a file has been modified. If it sees that a file has had a modified date change it runs the tests automatically. Also rspec is what autotests runs when it detects a file change.
Getting the Gems you Need
To get started you need to add these lines to your Gemfile, and do a bundle install so you can install the appropriate gems. It is recommended to put them in the development and test group so that you only runs them while doing development since in production those gems are no longer needed.
group :development, :test do
Don’t forget to run “bundle install”
Install Rspec in your Rails app
Just run this in command line on your rails application
$ rails generate rspec:install
Now that you have your install done when you create a new controller it should generate all your rspec files for you, and you are ready to go with your new project. From here open up a fresh terminal window and run the command ‘autotest’ and it will start running your tests.
If you are only using rspec for a new project you can skip to part 3, writing your first couple of tests, but I recommend you read the next part as well to get a better understanding of what is going on.
Part 2: Adding Rspec to an Existing Project
If you haven’t used TestUnit for writing tests then you can just delete the /test folder out of your project it really doesn’t need to be there at this point since all of our tests are going to be in the /spec folder.
This next part needs a bit more detail since you already have a project going and need to know how to insert rspec into it, and start using it. Specific things are needed in specific places, very similar to rails opinionation.
The specific files that are needed for a controller test are “spec/controllers/person_controller_spec.rb”, “app/helpers/person_helper.rb”.
These can really have any name to them. In general your test file will have the same name as your controller, or model, with _spec at the end of the name. Also for each controller you need to have the main helper file as well since rspec does try call it when running tests.
Here is some code from both so you can at least have a place holder, for now. We won’t be actually using the helper class in this, but, again, I am talking about it because you do need it. In general the helper is generated for you if you use the command line generation tool for controllers.
module UsersHelper end
Below is a general idea of the layout of what your spec/ folder should look like in your project. It helps keep it organized and looking good.
You can probably guess what kind of tests go where. For naming the test files you take the name of the file you are wanting to test and just add _spec to it. So:
persons_controller.rb => persons_controller_spec.rb
users_controller.rb => users_controller_spec.rb
Finally, if you are going to be running any of your tests and your application hits a database then you will need to make sure you have your migration files up-to-date and working. If you get any errors in your tests, only if you get them, where it cannot find a table and/or column name run the following command. It runs the migrations agains the test database to make sure the structure is there.
$ RAILS_ENV="test" rake db:migrate
Part 3: Writing your first few tests
First, if autotest is not running go ahead and open up a command line window and run it.
Now you need actually write a couple of tests to make sure things are working. Here is some example test code we can use to test our users controller, a bit.
Add this to your users_controller_spec.rb file in the /spec/controllers/ folder.
require 'spec_helper' describe UsersController do describe "GET Index" do it "gets the index view" do get "index" response.status.should be 200 end it "gets the correct index view template" do get "index" response.should render_template("users/index") end end end
Those two tests should pass and you should get an output similar to:
Finished in 0.08076 seconds
2 examples, 0 failures
That result assumes you aren’t doing anything fancy with your controllers.
A note about the example code, which I’ll explain more in some other post, the get call actually makes an http get and response is what is returned from that get, so you can test based on that.
Rspec was really scary until i could get it broken down to simplest terms as I have laid out in this post. This really is the simplest form you can get it into when starting to use rspec with your projects. Understanding how some of these basics work really will help when getting started, especially if you are adding rspec to an existing project. Hopefully this will help others as much as it has helped me.