Using RSpec in Rails

As a ruby on rails developer, I’d like to promote the need for testing your applications. No matter how small or large your project, tests will be an invaluable tool to ensure working functionality as you add, delete and refactor code. And I can promise you, during the lifespan of your web app, your code will remain far from static. This is very obvious when you are working for a client. They change their minds constantly. But even if YOU are the client, as I have found myself to be as I work on my own, hobby rails sites, I’ve discovered that even I cannot resist the urge to make changes to my site, some that often break past functionality.

That’s where tests come in. As long as I have a test suite I can run against after each coding session, I can be safely assured that I will be alerted to any code failures. Broken tests will point me to the region of the app I’ve disturbed with my updates, and I can make appropriate changes to fix those areas, as well as write any new tests necessary to cover any new or updated code.

I have a confession to make. I’ve only just started to embrace the testing paradigm. As a self-admitting maverick programmer, I liked seeing results fast, and I noticed that tests require a good bit more time to write and maintain than just an app’s code base. But as I delve more deeply into Ruby on Rails, I’m quickly discovering that though tests take more time to write up front, over time, the investment yields long term time saved in that I have fewer pesky bugs to track down as the code base grows. And as most developers know, tracking bugs can be the most monotonous and time consuming of all programming tasks.

In the world of Ruby on Rails, I’ve found that RSpec is my favorite testing platform. It wires nicely into rails with just a few simple lines of code.

1) Add the following lines to your Gemfile. Notice that these gems are only added to the test group. Makes sense, as we only need them when we are running our app in test mode.

group :test do
gem "rspec-rails", "~2.0"
gem "rspec-core"
end

2) From the terminal, cd into your rails app directory and run

bundle install

3) Finally, you’ll need to install rspec. Run the following from your app directory

rails generate rspec:install

The last script will create a spec directory in your rails application where you can begin adding tests. Since you’ll be using rspec, you can delete the built in test directory that rails gives you automatically. No need to leave clutter laying around.

I’ll add more posts soon with some actual rspec tests examples to real life applications. But for now, enjoy the following links:

Tagged ,

2 thoughts on “Using RSpec in Rails

  1. […] to use a versions of Rails specified in the above link. Hopefully you’re applications is well tested, but if not, be sure to check if the new version broke any previous […]

  2. […] the terminal, a factory will be created for that model. Just be sure to have the factory_girl and rspec gems installed in your application. Also, note that with the above code, the fixtures rails used to […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: