1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/lib/capybara/rspec.rb
Joshua Clayton 08a200b138 Allow RSpec view specs to leverage Capybara matchers
Capybara provides a number of helpful matchers for asserting against a
DOM structure with various selectors. RSpec's view specs focus on
asserting against specific markup; this change ensures this is easier
to do.

    RSpec.describe "todos/show.html.erb", type: :view do
      it "displays the todo title" do
        assign :todo, Todo.new(title: "Buy milk")

        render

        # without Capybara matchers - potentially ambiguous, doesn't
        # test markup, only raw text rendered
        expect(rendered).to contain "Buy milk"

        # with Capybara matchers
        expect(rendered).to have_css("header h1", text: "Buy milk")
      end
    end
2015-06-05 14:46:32 -04:00

33 lines
1.1 KiB
Ruby

require 'capybara'
require 'capybara/dsl'
require 'rspec/core'
require 'capybara/rspec/matchers'
require 'capybara/rspec/features'
RSpec.configure do |config|
config.include Capybara::DSL, :type => :feature
config.include Capybara::RSpecMatchers, :type => :feature
config.include Capybara::RSpecMatchers, :type => :view
# A work-around to support accessing the current example that works in both
# RSpec 2 and RSpec 3.
fetch_current_example = RSpec.respond_to?(:current_example) ?
proc { RSpec.current_example } : proc { |context| context.example }
# The before and after blocks must run instantaneously, because Capybara
# might not actually be used in all examples where it's included.
config.after do
if self.class.include?(Capybara::DSL)
Capybara.reset_sessions!
Capybara.use_default_driver
end
end
config.before do
if self.class.include?(Capybara::DSL)
example = fetch_current_example.call(self)
Capybara.current_driver = Capybara.javascript_driver if example.metadata[:js]
Capybara.current_driver = example.metadata[:driver] if example.metadata[:driver]
end
end
end