Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.2, assert_select, substitution value regexes #19098

Closed
jrochkind opened this issue Feb 26, 2015 · 13 comments
Closed

4.2, assert_select, substitution value regexes #19098

jrochkind opened this issue Feb 26, 2015 · 13 comments

Comments

@jrochkind
Copy link
Contributor

I think the documentation says that assert_select still supports substitution values, and regex substitution values, in Rails 4.2.

Use substitution values
assert_select "ol>li#?", /item-\d+/

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

When I try an example like the second one, however, I get:

DEPRECATION WARNING: The assertion was not run because of an invalid css selector.
unexpected '0' after 'equal'

Is there a way to still use substitution values with regexes? Alternately, is there another way to use assert_select to check for an element which has an attribute that matches a regex?

@kaspth
Copy link
Contributor

kaspth commented Feb 26, 2015

It still does. You just need to add quotes around the question mark: http://edgeguides.rubyonrails.org/4_2_release_notes.html#assert-select.

@jrochkind
Copy link
Contributor Author

Aha, thanks!

I read those release notes (several times!) in trying to see if there was an issue covered by release notes, but since the release notes didn't specifically include an example of ? used for substitution, or mention substitution at all, it somehow didn't penetrate that "Values in attribute selectors may need to be quoted if they contain non-alphanumeric characters" meant ? when used for substitution was a "non-alphanumeric character" that needed to be quoted.

The docs I linked to above still need to be corrected, so their examples of substitution use quotes around the question mark, don't they? The examples in the docs will not work.

@jrochkind
Copy link
Contributor Author

And I have no idea how you can use quotation marks to make this example from the docs work:

assert_select "ol>li#?", /item-\d+/

The quotation marks seem only to apply when you are inside attribute brackets in the selector... is substitution now only supported inside attribute brackets?

@jrochkind
Copy link
Contributor Author

And actually I can't get them to work even inside an attribute selector. I try:

assert_select "a.bd-direct-link[href='?']", /link_router/

I get:

 Expected at least 1 element matching "a.bd-direct-link[href='0']", found 0

I think substitutions with regexes may in fact not be working? I can try to put together a demo reproduction, but I'm not sure the best way to make a demo repro for assert_select, if there's a way to make an isolated reproduction app that mocks a given response to be used against assert_select. Anyone have any hints?

I think perhaps this issue should be re-opened. I don't think it's the quote issue.

@kaspth
Copy link
Contributor

kaspth commented Feb 26, 2015

Oh, sorry, I wasn't being clear. You have to use double quotes.

@kaspth
Copy link
Contributor

kaspth commented Feb 26, 2015

You're right about the documentation needing an update.

Do you (or anyone else watching) want to submit a PR for that? The examples should be:

assert_select %{[name="?"]}, /.+/
assert_select %{ol>li[id="?"]}, /item-\d+/

@jrochkind
Copy link
Contributor Author

It is not working for me with double quotes either. I really don't think it's a quoting issue, I think it's an issue with regexes and substitution in assert_select.

assert_select 'a.bd-direct-link[href="?"]', /link_router/

# => Expected at least 1 element matching "a.bd-direct-link[href="0"]", found 0..

Can you make substiution with a regex work?

For me substitution with a literal works -- and does not in fact require quotes.

    assert_select 'a.bd-direct-link[href=?]', "adfadf"
    # Yes, it didn't match, but the error message says it was checking the right thing,
    # unlike for the regex example
    # => Expected at least 1 element matching "a.bd-direct-link[href="adfadf"]", found 0

I think you are mistaken. Quotes are not in fact needed for ? substitution in assert_select -- so the docs may not need an update.

However, ? substitution with a regex is not working, with or without quotes.

@kaspth
Copy link
Contributor

kaspth commented Feb 26, 2015

I'm sorry, I should've looked more into this. I had completely forgotten the syntax had changed for 4.2. Here's an excerpt from the Rails Dom Testing documentation which has the right syntax:

#   # Use substitution values
#   assert_select "ol>li:match('id', ?)", /item-\d+/
#
#   # All input fields in the form have a name
#   assert_select "form input" do
#     assert_select ":match('name', ?)", /.+/  # Not empty
#   end

@kaspth
Copy link
Contributor

kaspth commented Feb 26, 2015

@fxn do you know why there's still documentation being generated for this here? The file was extracted in 4.2 and is empty: https://github.com/rails/rails/blob/29a10173e235ea495634de39f3dadd2da2e28fd4/actionpack/lib/action_dispatch/testing/assertions/selector.rb.

@jrochkind
Copy link
Contributor Author

Thanks @kaspth, let's see if I can succesfully figure out how to use that....

 assert_select 'a.bd-direct-link:match("href", ?)', /link_router/

Worked! Okay. Note it's got nothing to do with quotes, the ? does not need to be in quotes (and possibly wouldn't work right if it were).

It would have been nice if this had been in the release notes somewhere, but ce la vie (and maybe it is somewhere I missed).

I think it does need to be doc'd and the old wrong docs removed. I'm not sure how someone would ever figure this out without docs (or diving into the source/tests, I guess). Sorry, I don't think I can commit to a doc PR, I'm still not exactly clear on how this functionality works in general, and I'm a bit frustrated after having so much trouble getting my own tests to work in my own code.

@jrochkind
Copy link
Contributor Author

Oops, there IS a bug in the non-matched message though.

  assert_select 'a.bd-direct-link:match("href", ?)', /link_router/

Passes, because I did have a link like that. However, if I change it to:

  assert_select 'a.bd-direct-link:match("href", ?)', /not_existing_link_router/

I get the test failure message:

   Expected at least 1 element matching "a.bd-direct-link:match("href", 0)", found 0..

That doesn't make any sense, does it? Shouldn't it say something like:

   Expected at least 1 element matching "a.bd-direct-link:match("href", /not_existing_link_router/)", found 0..

?

@kaspth
Copy link
Contributor

kaspth commented Feb 26, 2015

I've fixed the misleading test failure message on Rails Dom Testing at rails/rails-dom-testing@f2a7eb4.

Note, it won't be exactly as you want because converting Regexps to strings makes Ruby wrap it in (?-mix:REGEXP). But it's as close as we're going to get.

Expected at least 1 element matching "div:match('id', "(?-mix:hello)")", found 0..
Expected 0 to be >= 1.

I'll look into updating the release notes tomorrow.

@rafaelfranca can you ship a new version of the gem? I don't have access to it on RubyGems.

@rafaelfranca
Copy link
Member

@kaspth done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants