Permalink
Browse files

Added DSL for acceptance tests in RSpec

  • Loading branch information...
Jonas Nicklas and Nicklas Ramhöj
Jonas Nicklas and Nicklas Ramhöj committed Feb 11, 2011
1 parent f5552df commit f4897f890d8dd33215fef238902988e8823a6539
Showing with 79 additions and 0 deletions.
  1. +16 −0 README.rdoc
  2. +1 −0 lib/capybara/rspec.rb
  3. +21 −0 lib/capybara/rspec/features.rb
  4. +34 −0 spec/rspec/features_spec.rb
  5. +7 −0 spec/rspec_spec.rb
View
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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

This comment has been minimized.

Show comment
Hide comment
@ravicious

ravicious Feb 11, 2011

Capybara ate Steak, didn't it?

ravicious replied Feb 11, 2011

Capybara ate Steak, didn't it?

@jnicklas

This comment has been minimized.

Show comment
Hide comment
@jnicklas

jnicklas Feb 11, 2011

Collaborator

Yes it did :)

Collaborator

jnicklas replied Feb 11, 2011

Yes it did :)

@ricn

This comment has been minimized.

Show comment
Hide comment
@ricn

ricn Feb 11, 2011

Haha awesome!

ricn replied Feb 11, 2011

Haha awesome!

@ivanacostarubio

This comment has been minimized.

Show comment
Hide comment
@ivanacostarubio

ivanacostarubio replied Feb 11, 2011

OHH YES!

@sobrinho

This comment has been minimized.

Show comment
Hide comment
@sobrinho

sobrinho Feb 11, 2011

Contributor

interesting :)

Contributor

sobrinho replied Feb 11, 2011

interesting :)

@mguterl

This comment has been minimized.

Show comment
Hide comment
@mguterl

mguterl replied Feb 11, 2011

Nice!

@ryw

This comment has been minimized.

Show comment
Hide comment
@ryw

ryw replied Feb 11, 2011

+1

@aka47

This comment has been minimized.

Show comment
Hide comment
@aka47

aka47 Feb 11, 2011

i love capybara steak barbecue..

aka47 replied Feb 11, 2011

i love capybara steak barbecue..

@hubertlepicki

This comment has been minimized.

Show comment
Hide comment
@hubertlepicki

hubertlepicki Feb 13, 2011

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.

hubertlepicki replied Feb 13, 2011

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

This comment has been minimized.

Show comment
Hide comment
@luigi

luigi Feb 17, 2011

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

sunlightlabs/datajam@a505ccd

luigi replied Feb 17, 2011

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

sunlightlabs/datajam@a505ccd

@exviva

This comment has been minimized.

Show comment
Hide comment
@exviva

exviva Feb 21, 2011

Contributor

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

Contributor

exviva replied Feb 21, 2011

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

@jnicklas

This comment has been minimized.

Show comment
Hide comment
@jnicklas

jnicklas Feb 22, 2011

Collaborator

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

Collaborator

jnicklas replied Feb 22, 2011

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

@exviva

This comment has been minimized.

Show comment
Hide comment
@exviva

exviva Feb 22, 2011

Contributor

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

Contributor

exviva replied Feb 22, 2011

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

@jnicklas

This comment has been minimized.

Show comment
Hide comment
@jnicklas

jnicklas Feb 22, 2011

Collaborator

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.

Collaborator

jnicklas replied Feb 22, 2011

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

This comment has been minimized.

Show comment
Hide comment
@exviva

exviva Feb 22, 2011

Contributor

Thanks, that dispelled all confusion.

Contributor

exviva replied Feb 22, 2011

Thanks, that dispelled all confusion.

Please sign in to comment.