Search code examples
ruby-on-railstestingminitest

Test for consecutive HTML elements in Rails view


I wish to run a test* to check for presence of something like this:

<tr>
    <td> name </td> <td> date </td>
</tr>

Code, such as below:

assert_select "tr" do
  assert_select "td",  name
  assert_select "td",  date
end

looks plausible, but is not correct, as the below for example (which is not the match required) would also pass:

<tr>
    <td> name </td>
</tr>
<tr>
    <td> date </td>
</tr>

I’m struggling to see how this should be approached from the documentation of assert_select.

Thank you

Daniel

  • within a default Rails integration test (I believe this means MiniTest)

Solution

  • I ended up using a regexs on the result of a css_select, which seems a bit inelegant, but worked for my purposes. If there is a better way I'd be interested to hear it. I used something like this:

    pars = css_select "tr"
    regexs = /<td>#{name}<\/td>.*<td>#{date}<\/td>/m
    match = false
    pars.each { |i| if i.to_s =~ regexs then match = true end}
    assert match