需要为链接图标重写Rspec测试。无法弄清楚如何正确引用元素
我目前的默认测试:
describe "Destroy action" do
it {should have_link('Remove project', href: project_path(Project.first))}
it "should be able to delete project" do
expect do
click_link('Remove project', match: :first)
end.to change(Project, :count).by(-1)
end
end
删除图片图标的帮助方法:
def delete_icon(height, width)
link_to image_tag('delete.png', alt: 'Remove project', id:"delete_logo", height: "# {height}", width: "#{width}"),@project, method: :delete, data: {confirm: "Burn it to the ground"}
end
我的部分观点:
%div.comments_sidebar
%h2 #{@project.title} #{edit_icon} #{delete_icon("700px", "30px")}
答案 0 :(得分:3)
首先,我认为你不应该在帮助器中使用实例变量。所以,我会像那样重写你的助手方法:
def delete_icon(project, height = <default height>, width = <default width>)
link_to image_tag('delete.png', alt: 'Remove project', id: "delete_logo", height: height, width: width), project, method: :delete, data: { confirm: "Burn it to the ground" }
end
在你的测试中,使用nokogiri
gem,你可以做类似的事情:
describe "delete_icon(project, height, width)" do
let(:project) { <create a project> }
it "creates a link with an image to remove given project" do
link = Nokogiri::HTML(helper.delete_icon(project)).css('a')[0]
image = link.css('img')[0]
expect(link.attributes['href'].value).to eq(project_path(project))
# check any other attributes for the link...
expect(image.attributes['src'].value).to eq('/assets/delete.png')
# check any other attributes for the image...
end
end
您可以添加更多将特定高度和/或宽度传递给helper方法的示例,并将此断言放在单独的示例中。