在has_selector传递的地方,Rspec has_link失败

时间:2013-10-08 10:13:20

标签: ruby-on-rails ruby rspec capybara

我在requests / pages_spec.rb

下进行了这两项测试

pages_spec.rb

require 'spec_helper'

describe "Pages" do
    subject { page } 
    before { visit root_path }

    describe "Home page" do
        it { should have_selector('a', text: 'Post your property')}
        it { should have_link('Post your property', href: new_apartment_path)}
    end
end

第一次测试通过,第二次测试失败说:

expected link "Post your property" to return something

这是html:

<a href="/en/apartment/new">Post your property</a>

知道测试失败的原因吗?谢谢! URI

2 个答案:

答案 0 :(得分:1)

也许你的should have_link的背景错了。您可以使用capybara gem的save_and_open_page测试它,如下面的代码,并检查该测试点的html。

require 'spec_helper'

describe "Pages" do
    subject { page } 
    before { visit root_path }

    describe "Home page" do
        it { should have_selector('a', text: 'Post your property')}
        save_and_open_page
        it { should have_link('Post your property', href: new_apartment_path)}
    end
end

答案 1 :(得分:0)

感谢您的帮助!这就是最终解决它的原因:

问题在于因为i18n而无法识别命名路径new_apartment_path。

是什么解决了这个问题:

let(:locale) { 'en' } 

并将测试更改为:

it { should have_link('Post your property', href: new_apartment_path(locale))}