Posts Tagged ‘Webrat’


Jumping on the Rails Pt.2 – RSpec, Cucumber & Webrat

June 27, 2009

Well it’s been a while since I’ve written anything really technical & seeing as Rails is taking up a lot of my personal time, I thought I made sense to put some of my newly found knowledge together.

I thought it best to not go over material that has already been done and very well I might add, I’ll just focus on the things have have interested me most & how to take advantage of them in real world situations. This and the next few posts will focus on developing and application using Cucumber, Rails & Webrat to drive the development process. Before I go on, I’d say that none of this would be possible if not for The RSpec book, I’ve read it for a couple times whilst the beta has been floating about and recommend the book to everyone developing in Rails, if only to see what it has to offer.

Lets start of with a basic application. StoryBoard, allows users to create & manage projects and their features with a focus on capturing features that can be used with Cucumber.


Now we need to setup our BDD environment

script/plugin install --force git:// &&
script/plugin install --force git:// &&
script/plugin install --force git:// &&
script/plugin install --force git:// &&
script/generate rspec &&
script/generate cucumber

Now that’s all we really need to setup our BDD environment, let start with a basic feature to get our feet wet:

Feature: Users must be able to register to the site

In order to allow new users to join the site they need to be able to view the home page & register.
As a guest user
I want to be able to register to the site so that I can create new scenarios for a project

Scenario: A user must be able to view the welcome page
Given I am not logged in
When I visit the home page
Then I should see the sign up link

Seems reasonable, users should be able to view the home page and see a sign up link, after all no one can register without one.

Now a nice little gift that comes with our plugins is autospec, I added the following to my .bash_profile

export AUTOFEATURE=true

This allows me to test both my specs & step definitions.

Starting script/autospec allows us to run our tests automatically on ever save, which can be a nice time saver for those who forget to test after a change.

You can implement step definitions for undefined steps with these snippets:
1 scenario (1 undefined)

3 steps (2 undefined, 1 passed)

You can implement step definitions for undefined steps with these snippets:

Given /^I am not logged in$/ do

When /^I visit the home page$/ do

Then /^I should see the sign up link$/ do

Cucumber is nice enough to give us code snippets to use in our step definitions, lets start with the first.

Create the file registration_steps.rb

We’ll cheat here seeing as we don’t need to set anything up, it passes.

Given /^I am not logged in$/ do

That gets our first part, lets move on to the next, again cucumber will nicely give us the next step definition, we’ll modifiy to look like the code below.

To take advantage of webrat within our specs we’ll need to add the following to our spec_helper.rb

require 'webrat'
config.include Webrat::Matchers, :type => :views

It should now look something like this:

ENV["RAILS_ENV"] ||= 'test'
require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
require 'spec/autorun'
require 'spec/rails'
require 'webrat'

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

Spec::Runner.configure do |config|
config.use_transactional_fixtures = true
config.use_instantiated_fixtures  = false
config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
config.include Webrat::Matchers, :type => :views

Now for the next step definition

When /^I visit the home page$/ do
visit '/'

Ok, we obviously need to setup a route for this one.

map.root :controller => "welcome"

We get another error saying:

uninitialized constant WelcomeController (NameError)

Okay we know we don’t have a controller, how about we cheat and make one just to make things pass for now. Especially as we don’t want to get bogged down with detail.

script/generate rspec_controller Welcome index

That will basically create our welcome page for us. We can now move on to the next step definition.

Then /^I should see the sign up link$/ do
response.should contain 'Sign up'

Now our tests are back to failing again, we’ll move to the controllers view to iron out our functionality.

Own spec_controller was nice enough to create our view for us, lets open up the view spec and add a spec.

#Delete this example and add some real ones or delete this file
it "should tell you where to find the file" do
response.should have_tag('p', %r[Find me in app/views/welcome/index])

Is our first test in our view spec, lets replace this with something more meaningful.

it "should have a sign up link" do
response.should have_selector("a", :content => "Sign up")

Here we just want to make sure that we have a page where we can sign the user up. Let get it to pass, remove the index.html from the public directory, the add the code below to your Welcome controllers index view.

<a href="/signup">Sign up</a>

Nice and simple, right. Now we’re still got alot of work to do but that is what the other stories are for ;), we’ll follow up with those in the next post.