将 Capybara 与 Rspec 一起使用 查看规格

Using Capybara with Rspec view specs

根据我的 Rspec 视图规范中的 this I think I should be able to use Capybara's has_field? 匹配器:

RSpec.describe "teams/edit", type: :view do
  let(:team) { FactoryGirl.create(:team, name: "Team") }

  it "renders the edit team form" do
    assign(:team, team)

    render

    expect(rendered).to has_field?("Name")
  end

end

以上失败:

Failure/Error: expect(rendered).to has_field?("Name")
     NoMethodError:
       undefined method `has_field?' for #<RSpec::ExampleGroups::TeamsEdit:0x007f9cd40c82a8>

我已将 require 'capybara/rails' 添加到我的 spec_helper.rb 并且 gem 'capybara' 在我的 Gemfile 中。我是否漏掉了一些明显的东西?

Capybara 2.5 rspec 匹配器在视图规范中默认可用。如果使用 2.5 之前的版本,您可以添加

RSpec.configure do |config|
    config.include Capybara::RSpecMatchers, :type => :view
end

到您的 RSpec 配置。

您将这些匹配器用作

expect(rendered).to have_field('Name')

注意使用 have_field 而不是 has_field?