Skip to content
This repository
Browse code

Added DSL for acceptance tests in RSpec

  • Loading branch information...
commit f4897f890d8dd33215fef238902988e8823a6539 1 parent f5552df
authored February 11, 2011
16  README.rdoc
Source Rendered
@@ -119,6 +119,22 @@ RSpec's metadata feature can be used to switch to a different driver. Use
119 119
       it 'will switch to one specific driver', :driver => :celerity
120 120
     end
121 121
 
  122
+Capybara also comes with a built in DSL for creating descriptive acceptance tests:
  123
+
  124
+    feature "signing up" do
  125
+      background do
  126
+        User.make(:email => 'user@example.com', :password => 'caplin')
  127
+      end
  128
+
  129
+      scenario "signing in with correct credentials" do
  130
+        within("#session") do
  131
+          fill_in 'Login', :with => 'user@example.com'
  132
+          fill_in 'Password', :with => 'caplin'
  133
+        end
  134
+        click_link 'Sign in'
  135
+      end
  136
+    end
  137
+
122 138
 Note that Capybara's built in RSpec support only works with RSpec 2.0 or later.
123 139
 You'll need to roll your own for earlier versions of RSpec.
124 140
 
1  lib/capybara/rspec.rb
@@ -2,6 +2,7 @@
2 2
 require 'capybara/dsl'
3 3
 require 'rspec/core'
4 4
 require 'capybara/rspec/matchers'
  5
+require 'capybara/rspec/features'
5 6
 
6 7
 RSpec.configure do |config|
7 8
   config.include Capybara, :type => :request
21  lib/capybara/rspec/features.rb
... ...
@@ -0,0 +1,21 @@
  1
+module Capybara
  2
+  module Features
  3
+    def self.included(base)
  4
+      base.instance_eval do
  5
+        alias :background :before
  6
+        alias :scenario :it
  7
+      end
  8
+    end
  9
+  end
  10
+end
  11
+
  12
+def feature(*args, &block)
  13
+  options = if args.last.is_a?(Hash) then args.pop else {} end
  14
+  options[:capybara_feature] = true
  15
+  options[:type] = :request
  16
+  args.push(options)
  17
+
  18
+  describe(*args, &block)
  19
+end
  20
+
  21
+RSpec.configuration.include Capybara::Features, :capybara_feature => true
34  spec/rspec/features_spec.rb
... ...
@@ -0,0 +1,34 @@
  1
+require 'spec_helper'
  2
+require 'capybara/rspec'
  3
+require 'capybara/rspec/features'
  4
+
  5
+Capybara.app = TestApp
  6
+
  7
+feature "Capybara's feature DSL" do
  8
+  background do
  9
+    @in_background = true
  10
+  end
  11
+
  12
+  scenario "includes Capybara" do
  13
+    visit('/')
  14
+    page.should have_content('Hello world!')
  15
+  end
  16
+
  17
+  scenario "preserves description" do
  18
+    example.metadata[:full_description].should == "Capybara's feature DSL preserves description"
  19
+  end
  20
+
  21
+  scenario "allows driver switching", :driver => :selenium do
  22
+    Capybara.current_driver.should == :selenium
  23
+  end
  24
+
  25
+  scenario "runs background" do
  26
+    @in_background.should be_true
  27
+  end
  28
+end
  29
+
  30
+feature "Capybara's feature DSL with driver", :driver => :culerity do
  31
+  scenario "switches driver" do
  32
+    Capybara.current_driver.should == :culerity
  33
+  end
  34
+end
7  spec/rspec_spec.rb
@@ -45,3 +45,10 @@
45 45
     expect { visit('/') }.to raise_error(NoMethodError)
46 46
   end
47 47
 end
  48
+
  49
+feature "Feature DSL" do
  50
+  scenario "is pulled in" do
  51
+    visit('/foo')
  52
+    page.body.should include('Another World')
  53
+  end
  54
+end

15 notes on commit f4897f8

Rafał Cieślak

Capybara ate Steak, didn't it?

Jonas Nicklas
Owner

Yes it did :)

Richard Nyström

Haha awesome!

Ivan Acosta-Rubio

OHH YES!

Gabriel Sobrinho

interesting :)

Michael Guterl

Nice!

Ry Walker

+1

Tim Aßmann

i love capybara steak barbecue..

Hubert Łępicki

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 Montanez

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

sunlightlabs/datajam@a505ccd

Olek Janiszewski

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

Jonas Nicklas
Owner

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

Olek Janiszewski

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

Jonas Nicklas
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.

Olek Janiszewski

Thanks, that dispelled all confusion.

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