How to: getting a rails 3 beta project set up with Rspec and Cucumber written by: Lee Richmond posted on: 27 March 2010

How to: get a rails 3 beta project set up with Rspec and Cucumber

okay so I recently started my first Rails 3 project I knew there was probably going to be one or two issues getting up and running and one of the main issues I found was getting Rspec and Cucumber to play ball with rails 3, but never fear here is how I got everything working.

Step 1: Installing Rails 3 and setting up a new app

I'm not going to go into detail on this step purely because I think that if you can't improve on something then don't try, so for this step I used Ryan Bates' Railscasts on getting started with RVM and Rails 3 Beta which can be found here to get me up and running, For reference I am still using Ruby 1.8.7 for this tutorial and the app I'm building I have called "test_app".

Step 2: Setting up your test environment in your Gemfile

Once I'd done a bit of reading this was actually quite simple, all you need to do as Rails 3 uses bundler as standard is open the Gemfile for your app in TextMate if you are using Mac OSX or any other text editor of your choice and add the following:

1
2
3
4
5
6
7
8
9
10
11

group :test do
  gem "rspec"
  gem "rspec-rails", ">= 2.0.0.beta.1"
  gem "faker"
  gem "machinist"
  gem "sqlite3-ruby", :require => "sqlite3"
  gem 'capybara',         :git => 'git://github.com/jnicklas/capybara.git'
  gem 'database_cleaner', :git => 'git://github.com/bmabey/database_cleaner.git'
  gem 'cucumber-rails',   :git => 'git://github.com/aslakhellesoy/cucumber-rails.git'
end
as you can see I have also added Faker, Machinist, and Sqlite3-ruby these aren't necessary to get Rspec and Cucumber working but I like to use these for testing, Once you have finished editing your Gemfile hit save go open a new terminal window.

Step 3: its bundling time!

In your terminal window first cd into your application in my case this is:

1
2

cd code/test_app
then once you have done this run the following commands in your terminal:
1
2
3
4

bundle install
rails g rspec:install
rails g cucumber:skeleton --rspec --capybara
the first will be familiar to anyone who has used bundler before but basically "bundle install" will bundle all the gems you need for your application, the next "rails g rspec:install" replaces what was "script/generate rspec" in Rails 2.3 and finally "rails g cucumber:skeleton --rspec --capybara" this bootstraps your rails app with everything necessary to use these gems. If you now go back to TextMate and look in your project drawer you will see that you now have your cucumber "features" directory and your Rspec "spec directory".

Now if we go and try writing some Cucumber Stories/features and try running them we can see that they work, if you are new to Cucumber I found that once again Ryan Bates' Railscasts on the subject were extremely helpful:

Episode 155: Beginning with Cucumber
Episode 159: More on Cucumber

I hope this blog post helps anyone out there trying to get set up with Cucumber and Rspec in Rails 3.

comments

Simon

Simon posted on29 March 2010

Really useful, thanks Lee I shall be giving this a whirl when I get home!
Lee Richmond

Lee Richmond posted on29 March 2010

No problem, if you have any problems post them up on here and I'll do my best to help solve them :)
Rob

Rob posted on30 March 2010

Thanks for this. I am pretty sure this saved me hours and hours of frustration.
Rob

Rob posted on30 March 2010

One more bit of information. Being brand new to Cucumber (I have used RSpec, but not in the context of Rails) I tried to use webrat ( rails g cucumber:skeleton --rspec --webrat) since I see so much more on the web about it. This doesn't appear to work too well with the rails 3 and cucmber set up. I kept getting: undefined method `visit' for # (NoMethodError) when I would run the features. So, I went back and did it exactly like the instructions you posted (using capybara) and it works just fine. I figured I would post it just in case anyone else ran across this as well. Thanks again!
LeeR

LeeR posted on30 March 2010

Thanks for your input Rob, you are right there are still a few issues as far as I could see with WebRat and I had been meaning to try out capybara for a long time as I had heard good things about it, so saw this as the perfect opportunity. Glad to hear that my blog post helped you.
this will not be published (include http://)

Rss RSS Atom ATOM

<<back to articles

latest articles

categories

Follow_me Recommend Me Geekup-logo