-
Notifications
You must be signed in to change notification settings - Fork 0
Ruby on Rails
Cucumber Rails support built-in, and I really recommend using Webrat
in the step definitions. Here is how to get you started.
You can use both Rubygems or Rails’ plugin mechanism. We usually recommend Rubygems. You can read a longer discussion here.
The easiest is to install everything as a gem.
[sudo] gem install rspec rspec-rails cucumber webrat
important: As of this writing, Cucumber 0.1.13 depends on Webrat code that isn’t officially released as a gem on Rubyforge. In the meanwhile, install Aslak Hellesøy’s snapshot gem:
gem sources -a http://gems.github.com
[sudo] gem install aslakhellesoy-webrat
Ubuntu/Debian users: You need to install the following libs for nokogiri to install properly (from webrat home):
sudo apt-get install libxslt1-dev libxml2-dev
If you use gems, you have to make sure everybody on the team has the same version of the gems installed. Also note that you don’t have to use rspec and rspec-rails and webrat, but we suggest you use them unless you have good reasons to use something else.
Another option is to install the libraries as Rails plugins. If you use Git for your Rails app, here is how to do that:
git submodule add git://github.com/aslakhellesoy/cucumber.git vendor/plugins/cucumber
git submodule add git://github.com/brynary/webrat.git vendor/plugins/webrat
git submodule add git://github.com/dchelimsky/rspec.git vendor/plugins/rspec
git submodule add git://github.com/dchelimsky/rspec-rails.git vendor/plugins/rspec-rails
If your own Rails code is not in Git, just replace submodule add with clone.
If you’re behind a proxy, try to replace git:// with http:// and set the http_proxy environment variable.
The plugins’ dependencies must be installed separately:
gem install term-ansicolor treetop diff-lcs nokogiriAs a last resort, download tarballs from GitHub.
Create the rails project with the following command:
rails YourProjectNameDon’t forget to create the corresponding databases.
Write all commands in the next steps from this project’s path.
You’ll need a Rake task and a couple of files that configure Cucumber for use with Ruby on Rails and Webrat.
You create these with:
ruby script/generate cucumberTake a look at the generated files. If you need to, you can tweak them later.
It’s really, really recommended that you write your features by hand – in collaboration with your
customer / business analyst / domain expert / interaction designer. However, to get you started (and so you can see how to use
Webrat), you can use the feature generator to generate the first few features:
ruby script/generate feature Frooble name:string color:string description:text
This will generate a simple plain text feature with associated steps. Don’t get addicted to this
generator – you’re better off writing these by hand in the long run.
Important: The generated feature will fail unless you have set up a layout in your app. This is because Webrat fails to parse HTML
that isn’t well formed (i.e. has a single <html> root node). Here is a simple layout you can use, but I hope you have a better one yourself.
Just run:
rake featuresThis should result in failing scenarios, because you haven’t written any code yet (I hope).
Now it’s time to write some code, or generate some. Try this:
script/generate rspec_scaffold Frooble name:string color:string description:text
rake db:migrate
rake features
See Running Features for more info.
Since I recommend you verify outcomes (Then steps) by looking at the HTML, you might end up having some degree
of redundancy with view specs. I recommend you delete generated view specs if you run into too much maintenance
headaches and rely on the features instead of view specs. However, in some cases it can be handy to use both.
Some guidance for authentication is provided below. It is recommended that a new user is created, rather than loaded through fixtures or etc.
Given /logged in as "(.*)"$/ do |login|
@current_user = User.create!(
:login=>login, :password => 'generic',
:email=>"#{login}@example.com", :password_confirmation => 'generic'
)
# @current_user.activate! # :create syntax for restful_authentication w/ aasm. Tweak as needed.
visit "/login"
fill_in("login", :with => login)
fill_in("password", :with => 'generic')
click_button("Log in")
response.body.should =~ /Logged/m
end