Rspec,Capybara:模糊匹配,发现2个元素匹配字段'brief'

时间:2014-04-21 17:43:50

标签: javascript ruby-on-rails rspec capybara-webkit

我刚刚使用Jquery实现了内联表单显示/隐藏功能,而且我的一些规格都失败了。我发表评论的答案和问题,评论可以发布回复其他评论。显示表单后,添加评论'链接更改为'取消'点击'取消',表单被隐藏。

我的规格:

scenario 'on a answer', js:true do
    submit_answer @answer.brief
    submit_comment @comment_brief, '.answers'
    expect(page).to have_content(@idea_brief)
    expect(page).to have_content(@question_brief)
    expect(page).to have_content(@answer.brief)
    expect(page).to have_content(@comment_brief)
end
scenario 'on a comment', js: true do
    @commentreply = FactoryGirl.create(:comment)
    submit_comment @comment_brief, ".question"
    submit_comment @commentreply.brief, '.question-comments', 'Reply to comment'
    expect(page).to have_content(@question_brief)
    expect(page).to have_content(@comment_reply)
end

我认为这两个错误是相同的,可能与js内联表单hide / show有关。

我的失败:

1) Visitor submits a comment on a answer
 Failure/Error: submit_comment @comment_brief, '.answers'
 Capybara::Ambiguous:
   Ambiguous match, found 2 elements matching field "Brief"
 # ./spec/support/features/session_helpers.rb:52:in `submit_comment'
 # ./spec/features/comment_spec.rb:17:in `block (2 levels) in <top (required)>'

2) Visitor submits a comment on a comment
 Failure/Error: submit_comment @commentreply.brief, '.question-comments', 'Reply to   comment'
 Capybara::Ambiguous:
   Ambiguous match, found 2 elements matching field "Brief"
 # ./spec/support/features/session_helpers.rb:52:in `submit_comment'
 # ./spec/features/comment_spec.rb:33:in `block (2 levels) in <top (required)>'

我的部分答案形式:

<li class="media answer answer-<%= answer.id %>">

    <%= render partial: "evaluations/vote_wrapper", locals: {voteable: answer} %>


    <div class="media-body">
      <div class="media-item-body">
        <%= answer.brief %>
      <div class="pull-right">
        <%= render answer.user %>
      </div>
      </div>
      <ul class="media-list comments answer-comments">
        <% if answer.comments.count > 0 %>
          <%= render vote_order(answer.comments.includes(:user)) || "No comments found" %>
        <% end %>
        <div class="<%= answer.id %>">
          <p class="add comment">
             <%= link_to "Add comment", new_answer_comment_path(answer), remote: true %>
          </p>
          <p class="comment cancel" style="display:none">
            <%= link_to "Cancel", new_answer_comment_path(answer), remote: true %>
          </p>
        </div>
      </ul>
    </div>
</li>

最后,我的new.js.erb发表评论:

var selector = ".<%= j @commentable.class.to_s.downcase + '-' + @commentable.id.to_s + '  .media-body ' %>"
selector0 = selector + ".<%= j @commentable.class.to_s.downcase %>-comments"

var selector1 = ".<%= j @commentable.class.to_s.downcase%>"
var selector2 = ".<%= j @commentable.class.to_s.downcase + '-' + @commentable.id.to_s%>"
var selector3 = selector1 + selector2 + " div.<%= j @commentable.id.to_s%>"

if($(selector3 + " .add.comment").is(":visible")){ 
        if ($(selector0 + " .js-inline-form").length == 0) {
    $(selector0)
            .append("<li class='js-inline-form'><%= j render :partial => 'comments/form' %></li>")
    }
    else {
        $(selector0 + " .js-inline-form").remove()
        $(selector0) 
            .append("<li class='js-inline-form'><%= j render :partial => 'comments/form' %></li>")
}
    $(selector3 + " .add.comment").hide()
    $(selector3 + " .comment.cancel").show()

}
else if($(selector3 + " .comment.cancel").is(":visible")){
    $(selector0 + " li.js-inline-form").remove()
    $(selector3 + " .comment.cancel").hide()
    $(selector3 + " .add.comment").show()
}

另外,我有一个规范帮助函数:

def submit_comment(brief, selector='html', add_comment_link = 'Add comment')
  first(selector).click_link add_comment_link
  fill_in 'Brief', with: brief
  click_button 'Create Comment'
end

我将find()更改为first(),因为我看到人们在Capybara 2.0.1中遇到含糊不清和功能的问题。

不知道造成这种情况的原因是什么?当我进入服务器并测试相同的东西时,我没有任何问题。如果有任何其他代码有助于解决此问题,请告诉我们。谢谢。

0 个答案:

没有答案