0

I have just implemented an inline form show/hide function with Jquery and a few of my specs are failing. I post comments to answers and to questions, and comments can be posted in reply to other comments. When the form is shown, the 'add comment' link changes to 'cancel' and upon clicking 'cancel', the form is hidden.

My spec:

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

I assume the two errors are identical, probably having to do with the js inline form hide/show.

My failures:

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)>'

My partial form for answer:

<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>

And finally, my new.js.erb for comment:

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()
}

Also, I have a spec helper function:

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

I changed find() to first() because I saw people were having issues with ambiguity and that function in Capybara 2.0.1.

Don't know what's causing this to happen? When I go on a server and test the same thing, I have no issues. If there is any other code that would be helpful to resolve this issue, please let me know. Thank you.

Annika
  • 55
  • 2
  • 10
  • 1
    The exception suggests the problem is with the line `fill_in 'Brief', with: brief`. This would mean that there are multiple 'Brief' elements on the page. You need to make the locator more specific so that only one is matched. If you share the generated html and how you (as a user) would differentiate the two, we can help give a better locator. – Justin Ko Apr 22 '14 at 13:12
  • If you set `Capybara.match` to `:prefer_exact` , what happens then ? – RVM Apr 22 '14 at 15:37

0 Answers0