rails/rails-dom-testing

Placeholders not recognizeds by assert_select

everton opened this issue · 10 comments

Until Rails 4.1.6 I used placeholders to avoid quoting manually some attributes of #assert_select selector, like on this example:

assert_select 'form[action=?][method=?]', action, method

but now it is generating this error:

ArgumentError: I don't understand what you're trying to match

which not happens when not using the placeholders:

assert_select "form[action='#{action}'][method='#{method}']"

Will this placeholders feature be removed on stable 4.2.0 or is this a bug?

There never was a placeholders feature. What you're doing is an unintended side effect of how substitution values worked.
They've changed in Rails 4.2: https://github.com/rails/rails-dom-testing/blob/master/lib/rails/dom/testing/assertions/selector_assertions.rb#L161-L167

Sorry @kaspth, but if it was unintentional, I think I misunderstood this documentation:

"The selector may be a CSS selector expression (String), an expression with substitution values, or an HTML::Selector object."

# All input fields in the form have a name
assert_select "form input" do
  assert_select "[name=?]", /.+/  # Not empty
end

http://api.rubyonrails.org/classes/ActionDispatch/Assertions/SelectorAssertions.html#method-i-assert_select

You are looking the documentation of Rails < 4.2. This was changed now. See the new documentation @kaspth pointed.

Yes, it changed. But it should work with a string too. But you have to use :match operator. Are not string working with the :match operator?

This test in our test suite is passing:

    render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
    assert_select "div[id=?]", "1" do |elements|
      assert_equal 1, elements.size
    end

I didn't understood clearly how to deal with my example about the form using the :match selector.

If I use:

"form:match('action', ?):match('method', ?)"

it always give me this error:

DEPRECATION WARNING: The assertion was not run because of an invalid css selector.

About your test, it is exactly what we are using here, except that we are dealing with a (path) string on the attribute action and it is raising the error I reported:

ArgumentError: I don't understand what you're trying to match

@everton it sounds to me that action or method is an unexpected class here, and it actually has nothing to do with whether you're using the ? or :match spelling in the selector.

@matthewd thank you, your observation has lead me to understand the problem.

Sorry @ALL, I didn't realize that I was using a Symbol for the :method parameter and not a String. Now it is working perfectly.

So, it seems that the substitution with Strings still working.

@everton awesome!

From a quick look through history, it doesn't sound like symbols ever would've worked: rails/rails@3142502#diff-74f6318eecf03bd2173930ea13ad8202R213 -- so I guess this isn't a misbehaviour in previously-working-but-newly-upgraded code, as I'd originally inferred?

Anyway, if you're feeling adventurous, I think a PR to improve that exception message ("don't know how to match #{arg.class}", perhaps?) would be an excellent precaution for the next person to hit it.