使用带有Rspec视图规范的Capybara



根据这个我想我应该能够使用Capybara的has_field?matcher从我的Rspec视图规范:

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?

最新更新