Can someone help with an assert_select problem please?
I have:
<p>some text<br />some more text</p>
I see how to use assert_select to check that the text in the paragraph
is "some textsome more text" and I can check that the paragraph
contains a break. I cannot see how to check that the break is at the
right point in the text.
Colin Law wrote:
[...]
} I cannot see how to check that the break is at the
right point in the text.
What does the "right point" consist of? In other words, how (English,
not Ruby) would you define the break being at the right point?
Repeating my original post for clarity:
I have:
<p>some text<br />some more text</p>
The right point for the break is after 'some text' and before 'some more text'
As I said I can see how to use assert_select to check that the
complete text is there, and how to check that the break is there, but
I do not see how to check that the break is after 'some text' and
before 'some more text'. I am no doubt missing something obvious.
Of course in my real application the text is not as in this simplified example.
Perhaps I should clarify further. In my functional test I currently
have something like
assert_select "p", "some textsome more text" # checks the text is correct
assert_select "p>br", 1 # checks p contains br
What I am lacking is a check that the <br /> is in the correct place
in the text.
The right point for the break is after 'some text' and before 'some more text'
How about assert_select "p", /some text.*<br[^>]*>some more text/m ?
BTW, you shouldn't be using <br/> in the self-closing form unless you're
generating XHTML (it is not actually valid HTML), and you shouldn't be
generating XHTML unless you're serving it with the XHTML MIME type -- at
which point IE won't understand the document! So in general, HTML 4 is
the way to go (the html_output plugin will help). See the recent thread
where Rimantas convinced me of this (admittedly after some argument from
me).
The right point for the break is after 'some text' and before 'some more text'
How about assert_select "p", /some text.*<br[^>]*>some more text/m ?
No that doesn't work. The br tag does not appear in the text for the
p tag. This can be seen from the fact that
assert_select "p", "some textsome more text"
passes.
I tried it just to check and it said
/some text.*<br[^>]*>some more text/m expected but was
some textsome more text
BTW, you shouldn't be using <br/> in the self-closing form unless you're
generating XHTML (it is not actually valid HTML), and you shouldn't be
generating XHTML unless you're serving it with the XHTML MIME type -- at
which point IE won't understand the document! So in general, HTML 4 is
the way to go (the html_output plugin will help). See the recent thread
where Rimantas convinced me of this (admittedly after some argument from
me).
For anyone coming to this thread without the history I am trying to
use assert_select to verify the html
<p>some text<br>some more text</p>
I found a solution to this by adding span tags round my text
fragments, so that the html is now
<p><span>some text</span><br><span>some more text</span></p>
Then I can test it with
assert_select "p>span:first-of-type", "some text"
assert_select "p>span:first-of-type+br", 1
assert_select "p>span:nth-of-type(2)", "some more text"
This works but feel there must be a better solution than adding extra
tags to the html just so that it can be tested.
Colin Law wrote:
[...]> I found a solution to this by adding span tags round my text
fragments, so that the html is now
<p><span>some text</span><br><span>some more text</span></p>
[...]
This works but feel there must be a better solution than adding extra
tags to the html just so that it can be tested.
Indeed. Try using the regexp I gave you as a simple string match on
response.body , or perhaps go for an XPath-based solution (with RSpec,
that would be rspec_hpricot_matchers, but I don't know if that works
without RSpec).