Ruby on Rails, assert_select and Nokogiri::CSS::SyntaxError: unexpected '?' after ', '

← Back

When writing a new Rails 7 web application, I hit the following error in tests:

Error:
BibCategoriesControllerTest#test_new_category_form:
Nokogiri::CSS::SyntaxError: unexpected '?' after ', '
    (eval):3:in `_racc_do_parse_c'
    (eval):3:in `do_parse'
    test/controllers/bib_categories_controller_test.rb:62:in `block (3 levels) in <class:BibCategoriesControllerTest>'
    test/controllers/bib_categories_controller_test.rb:60:in `block (2 levels) in <class:BibCategoriesControllerTest>'
    test/controllers/bib_categories_controller_test.rb:58:in `block in <class:BibCategoriesControllerTest>'

For this test:

assert_select 'option:match("value", ?)[selected]', category_1.id

This is a new syntax of assert_select. Long time ago, this used to look like this and work:

assert_select 'option[value=?][selected]', category_1.id

Various web searches didn’t yield anything useful (if anything at all). So I tried random things, until I found the cause. The parameter must be a string. So here comes the fixed, working test:

assert_select 'option:match("value", ?)[selected]', category_1.id.to_s

Ta-daa.


Tagged with: Ruby on Rails

Written: 2022-05-17