1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

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
This commit is contained in:
Joshua Clayton 2015-06-05 14:16:13 -04:00
parent ae662272f6
commit 08a200b138
3 changed files with 23 additions and 1 deletions

View file

@ -167,7 +167,7 @@ describe 'some stuff which requires js', :js => true do
end
```
Finally, Capybara also comes with a built in DSL for creating descriptive acceptance tests:
Capybara also comes with a built in DSL for creating descriptive acceptance tests:
```ruby
feature "Signing in" do
@ -203,6 +203,20 @@ end
`background` is an alias for `before`, `scenario` for `it`, and
`given`/`given!` aliases for `let`/`let!`, respectively.
Finally, Capybara matchers are supported in view specs:
```ruby
RSpec.describe "todos/show.html.erb", type: :view do
it "displays the todo title" do
assign :todo, Todo.new(title: "Buy milk")
render
expect(rendered).to have_css("header h1", text: "Buy milk")
end
end
```
## Using Capybara with Test::Unit
* If you are using Rails, add the following code in your `test_helper.rb`

View file

@ -7,6 +7,7 @@ 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.

7
spec/rspec/views_spec.rb Normal file
View file

@ -0,0 +1,7 @@
require 'spec_helper'
RSpec.describe "capybara/rspec", type: :view do
it "allows matchers to be used on strings" do
expect(%{<h1>Test header</h1>}).to have_css("h1", text: "Test header")
end
end