RSpec is Fun

I've been using RSpec with my latest project, and it's a welcome change from the traditional Test::Unit for Ruby.

RSpec is a more natural way to write tests for your software. It seems to be much easier to compose tests, and anything that makes testing easier is OK in my book.

A specific example of why I'm falling for RSpec:

You can easily group common tests together, and include this grouping in your main tests. Of course you can do this in any number of ways with Test::Unit, but RSpec makes it so clean and obvious.

Here's an example of a very common set of tests that I need to run again all my controllers:


describe "Requires Authorization", :shared => true do
describe "when not logged in" do

describe "when accessing index page" do
it "should redirect to login page" do
get 'index'
response.should redirect_to(new_session_path)
end
end

describe "when accessing new database page" do
it "should redirect to login page" do
get 'new'
response.should redirect_to(new_session_path)
end
end

describe "when accessing show database page" do
it "should redirect to login page" do
get 'show'
response.should redirect_to(new_session_path)
end
end

describe "when accessing destroy database page" do
it "should redirect to login page" do
get 'destroy'
response.should redirect_to(new_session_path)
end
end

end
end


In each of my controller tests, I only need to include:


describe DatabasesController do

it_should_behave_like "Requires Authorization"


Take a look at that! Reads just like English, I would say.

If you haven't made the jump to RSpec, I'd seriously recommend you give it a shot. You don't need to abandon all of you existing unit tests. Both sets of tests will run just fine in your Rails project.

Popular posts from this blog

Lists and arrays in Dart

Converting Array to List in Scala

Null-aware operators in Dart