Rails RSpec - 测试has_link时出错

时间:2015-04-03 04:59:06

标签: ruby-on-rails ruby specifications link-to

我想测试我的主页。不应该链接到自己。 为什么不是真的?

规格/特征/ main_pages_spec.rb

require 'spec_helper'

RSpec.describe "main pages", :type => :features do

  subject { page }
  ...
  context "root page" do

    it "should have the link 'Home'" do
      visit root_path
      expect(page).to have_link('Home', href: '#')
    end
  end
  ...
end

应用程序/视图/ main_pages / home.html.erb

...
<%=link_to("Home", "#") %>
...

=&GT;

main pages root page should have the link 'Home'
     Failure/Error: expect(page).to have_link('Home', href: '#')
       expected #has_link?("Home", {:href=>"#"}) to return true, got false
     # ./spec/features/main_pages_spec.rb:11:in `block (3 levels) in <top (required)>'

更新: 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

 context "root page" do
    before { visit root_path }
    it "should have the link 'Home'" do      
      expect(page).to have_link('Home', href: '#')
    end
  end