Ruby on rails Rspec have_链接失败,其中have_选择器通过
我在requests/pages_spec.rb下进行了这两项测试 页面规格rbRuby on rails Rspec have_链接失败,其中have_选择器通过,ruby-on-rails,ruby,rspec,capybara,Ruby On Rails,Ruby,Rspec,Capybara,我在requests/pages_spec.rb下进行了这两项测试 页面规格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 { sho
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也许您的
的上下文应该有链接
是错误的。您可以使用capybara gem的save_和\u 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
谢谢你的帮助!这就是最终解决问题的原因: 问题在于,由于i18n的原因,无法识别命名的route new_apartment_path 解决问题的办法是添加一行:
let(:locale) { 'en' }
并将测试更改为:
it { should have_link('Post your property', href: new_apartment_path(locale))}
您确定
新建公寓路径设置正确吗?如果不正确,第一个测试将通过,第二个测试将失败,正如您所看到的。我的URL有一个:locale前缀。也许这就是问题的根源?例如,为了使用user\u path,我必须以:user\u path(:id=>user.id)的形式传递它才能工作。我不知道如何使用新的公寓路径来解决这个问题……你能运行测试它{应该有链接('Post your property',href:“/en/plant/new”)}
来确认问题是新公寓路径的问题吗?问题确实是新公寓路径的问题。我会发布我的解决方案。谢谢