Chris Buczkowski3,939 Points
Receiving 'ambiguous match, found 2 elements matching css "h1"' - rspec test error
somewhere in the course of this video I'm introducing the error below into my test, but I'm unsure how to fix it. up to this point, I've been able to clear out all of the errors, and was only getting those that Jason is getting in the course videos (aside from the ul/li -->> table/tr/td change stuff he did in this video which caused errors which I was able to figure out and fix).
here's the failure:
Failures: 1) Viewing todo items displays the title of the todo list Failure/Error: within("h1") do Capybara::Ambiguous: Ambiguous match, found 2 elements matching css "h1" # ./spec/features/todo_items/index_spec.rb:9:in `block (2 levels) in <top (required)>'
I was able to comment stuff out until I narrowed it down to this line in my application.html.erb file:
<h1><%= link_to "Odot", root_path %></h1>
I'm not sure where I need to look.
one thing I did try was changing the tags there to h2 tags instead of h1, and that cleared up the testing error...
Finished in 0.96495 seconds 21 examples, 0 failures
but it causes the actual test criteria to fail (which I sort of vaguely grasp)
Failed examples: rspec ./spec/features/todo_items/index_spec.rb:7 # Viewing todo items displays the title of the todo list
I think I kind of understand what's happening. there's just the one h1 tag in the application.html.erb file, but somewhere, another h1 tag is getting pulled into the page as well, from another file somewhere, but where I'm most lost (I think) is where that is pulling from, and how to correct it.
so I guess I have three questions...
first, where should I be looking to fix this (and more specifically, how)?
second, in the 'real world', would this be the sort of thing that would normally be handled by adding an id attribute to the html tag, in order to prevent the test from being confused as to which h1 element to look at?
third, why is it referencing CSS, and not HTML as the cause of the error?
Maciej Czuchnowski36,429 Points
h1 is probably located in the todo_list show view or todo_items index view. Yes, normally you would add some unique id or at least a class to your elements to eliminate ambiguity. It references css because
within can be used to look for DOM elements by their class and id as well. It is more common than using it only with an html tag, because it's very likely that there are more tags like this on the page which causes ambiguity errors ;).