I’ve been using Steak for almost a year now. It’s an RSpec extension by @cavalle to allow you to get up and running doing acceptance testing. Since then I wrote and talked about it a lot and it definitely helped me to get into acceptance testing without having to get out of RSpec and writing tests in English.
What a lot of people don’t know is that Steak is nothing more than a bunch of aliases and generators. This doesn’t mean that it doesn’t make your work easier or that it’s a scam, but it’s not a completely separate testing library. It’s essentially just a tool to make it easier to get Capybara running on RSpec.
Carnivore Capybara!!!1 Roaargh!
Back when Steak was first released, Capybara didn’t have any of the nice RSpec helpers it does now. A lot has changed since. Besides the helpers, it got its own RSpec acceptance testing DSL recently, essentially eating Steak’s functionality and turning it into a complete acceptance testing solution (on top of RSpec).
Acceptance testing with Capybara
The DSL is’nt officially released yet, but it will be available in Capybara 1.0. Right now, you’ll have to get it from git if you want to use it.
Let’s say you’re working on a Rails project using Bundler. Simply add RSpec and Capybara to your Gemfile
:
group :test do
gem 'rspec-rails'
gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git'
end
After running bundle install
, set up RSpec like you’re used to:
$ rails g rspec:install
create .rspec
create spec
create spec/spec_helper.rb
Capybara doesn’t have any generators for its new DSL, but that shouldn’t be a problem. Setting it up is quite easy, just add a file named spec/acceptance/acceptance_helper.rb
. In this file, we’ll require spec/spec_helper.rb
to make RSpec available. We’ll also require Capybara’s RSpec helper file:
require 'rspec'
require 'capybara/rspec'
Capybara will now automatically hook into RSpec and allow you to write your first test. See? I told you we weren’t going to need any generators. Let’s write a spec named spec/acceptance/articles_spec.rb
:
require 'acceptance/acceptance_helper'
feature "Articles", %q{
In order to have an awesome blog
As an author
I want to create and manage articles
} do
background do
Article.create!(:title => 'One')
Article.create!(:title => 'Two')
end
scenario "Article index" do
visit '/articles'
page.should have_content('One')
page.should have_content('Two')
end
end
As you can see, we’re using methods like feature
, background
and scenario
instead of context
, before
and example
, just like we did in Steak. Now, you can run your new spec like any other RSpec test:
$ rspec spec/acceptance/articles_spec.rb
There you go. A complete acceptance testing solution including the acceptance testing slang, without generators and just a couple of lines of code. What do you think? Do you have any ideas to make this more awesome? Definitely let me know!