我有一个rspec测试,我需要专门找到一个div块,从文章索引页面,对应一篇文章ID,找到代表销毁/编辑和文章链接的glyphicon。我在索引页面上有一个文章列表,所以我需要具体的css选择器。但是,我无法找到解决方案。我尝试了以下方法:
1)为文章部分中的每篇文章添加一个id标记,并使用'within'调用特定的div
文章部分:
<div class = "row" id="<%= article.id %>" >
....more code....
</div>
规格/特征/ article_spec.rb
describe 'navigate' do
let!(:user) { FactoryGirl.create(:user) }
let!(:article) { FactoryGirl.create(:article) }
before do
login_as(user, :scope => :user)
end
describe 'edit' do
before do
@article_to_edit = Article.create(title: 'Article to edit', summary: 'Summary of article to edit', description: 'Test to edit this article', user_id: user.id)
end
it 'edit and delete icon is visible to article owner from index page' do
visit articles_path
within '#{@article_to_edit.id}' do
expect(page).to have_css('.glyphicon-pencil')
expect(page).to have_css('.glyphicon-trash')
end
end
end
2)用以下代码替换'within'块以查找文章的特定href链接
expect(page).to have_link('', href: "/articles/#{@article_to_edit.friendly_id}/edit")
expect(page).to have_link('', href: "/articles/#{@article_to_edit.friendly_id}")
答案 0 :(得分:0)
Sting插值仅适用于带双引号的字符串。
只需更改
within '#{@article_to_edit.id}' do
到
within "#{@article_to_edit.id}" do
在您在问题中发布的代码示例中。并在使用articles/#{@article_to_edit.friendly_id}...