我有一个没有传递的循环'have_selector'测试,但使用Capybara.string进行更直接的测试。谁能解释一下呢?
require 'spec_helper'
describe "Item pages" do
subject { page }
describe "Item list" do
let(:user) { FactoryGirl.create(:user) }
before do
FactoryGirl.create(:item, user: user, description: "Testing")
FactoryGirl.create(:item, user: user, description: "Testing 2")
sign_in user
visit items_path
end
it { should have_link("Logout") }
it "should render the user's list of items" do
user.items.each do |item|
#expect(page).to have_selector("li##{item.id}", text: item.description)
Capybara.string(page.body).has_selector?("li##{item.id}", text: item.description)
end
end
end
end
当执行此测试时,注释掉的'expect()'测试没有通过,但是下面的测试确实如此。失败消息是:
Failure/Error: expect(page.body).to have_selector("li##{item.id}", text: item.description)
NoMethodError:
undefined method `has_selector?' for #<String:0x007fe2a9782dd0>
答案 0 :(得分:1)
错误的原因是因为您在has_selector?
课程上调用了String
。 has_selector?
来自Capybara::Node::Matchers
模块。
要解决此问题,请使用以下内容更新示例:
it "should render the user's list of items" do
user.items.each do |item|
#expect(page).to have_selector("li##{item.id}", text: item.description)
page.has_selector?("li##{item.id}", text: item.description)
end
end