Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added DSL for acceptance tests in RSpec

  • Loading branch information...
commit f4897f890d8dd33215fef238902988e8823a6539 1 parent f5552df
Jonas Nicklas and Nicklas Ramhöj authored
View
16 README.rdoc
@@ -119,6 +119,22 @@ RSpec's metadata feature can be used to switch to a different driver. Use
it 'will switch to one specific driver', :driver => :celerity
end
+Capybara also comes with a built in DSL for creating descriptive acceptance tests:
+
+ feature "signing up" do
+ background do
+ User.make(:email => 'user@example.com', :password => 'caplin')
+ end
+
+ scenario "signing in with correct credentials" do
+ within("#session") do
+ fill_in 'Login', :with => 'user@example.com'
+ fill_in 'Password', :with => 'caplin'
+ end
+ click_link 'Sign in'
+ end
+ end
+
Note that Capybara's built in RSpec support only works with RSpec 2.0 or later.
You'll need to roll your own for earlier versions of RSpec.
View
1  lib/capybara/rspec.rb
@@ -2,6 +2,7 @@
require 'capybara/dsl'
require 'rspec/core'
require 'capybara/rspec/matchers'
+require 'capybara/rspec/features'
RSpec.configure do |config|
config.include Capybara, :type => :request
View
21 lib/capybara/rspec/features.rb
@@ -0,0 +1,21 @@
+module Capybara
+ module Features
+ def self.included(base)
+ base.instance_eval do
+ alias :background :before
+ alias :scenario :it
+ end
+ end
+ end
+end
+
+def feature(*args, &block)
+ options = if args.last.is_a?(Hash) then args.pop else {} end
+ options[:capybara_feature] = true
+ options[:type] = :request
+ args.push(options)
+
+ describe(*args, &block)
+end
+
+RSpec.configuration.include Capybara::Features, :capybara_feature => true
View
34 spec/rspec/features_spec.rb
@@ -0,0 +1,34 @@
+require 'spec_helper'
+require 'capybara/rspec'
+require 'capybara/rspec/features'
+
+Capybara.app = TestApp
+
+feature "Capybara's feature DSL" do
+ background do
+ @in_background = true
+ end
+
+ scenario "includes Capybara" do
+ visit('/')
+ page.should have_content('Hello world!')
+ end
+
+ scenario "preserves description" do
+ example.metadata[:full_description].should == "Capybara's feature DSL preserves description"
+ end
+
+ scenario "allows driver switching", :driver => :selenium do
+ Capybara.current_driver.should == :selenium
+ end
+
+ scenario "runs background" do
+ @in_background.should be_true
+ end
+end
+
+feature "Capybara's feature DSL with driver", :driver => :culerity do
+ scenario "switches driver" do
+ Capybara.current_driver.should == :culerity
+ end
+end
View
7 spec/rspec_spec.rb
@@ -45,3 +45,10 @@
expect { visit('/') }.to raise_error(NoMethodError)
end
end
+
+feature "Feature DSL" do
+ scenario "is pulled in" do
+ visit('/foo')
+ page.body.should include('Another World')
+ end
+end

15 comments on commit f4897f8

@ravicious

Capybara ate Steak, didn't it?

@jnicklas
Owner

Yes it did :)

@ricn

Haha awesome!

@ivanacostarubio

OHH YES!

@sobrinho

interesting :)

@mguterl

Nice!

@ryw

+1

@aka47

i love capybara steak barbecue..

@hubertlepicki

Works perfect for me, the only thing worth mentioning is that in example filters you need to change ":type => :acceptance" to ":type => :request". This means you have to update paths.rb and helpers.rb in spec/acceptance/support directory when upgrading from Steak, otherwise those modules won't be included properly.

@luigi

Also, you need to require 'capybara/rspec' in your acceptance_helper.rb. The commit for my project:

sunlightlabs/datajam@a505ccd

@exviva

Except for the DSL, what is the difference between spec/acceptance and spec/requests?

@jnicklas
Owner

exviva: Honestly, I don't know. But RSpec and David Chelimsky seem to prefer :request, so that's what we went with.

@exviva

I see. So is the DSL mixed into the request specs as well? Is Capybara generally accessible in request specs?

@jnicklas
Owner

The DSL is entirely separate. It does set :type => :request, so anything you use the DSL on will automatically have that type.

If you require 'capybara/rspec', then Capybara will be available in all specs which have type :request or type :acceptance.

@exviva

Thanks, that dispelled all confusion.

Please sign in to comment.
Something went wrong with that request. Please try again.