Fearless Fool
2011-Jan-24 21:19 UTC
[rspec-users] [newbie] CSS navigation in controller response?
I''m hooked on RSpec after my first taste (thanks to
http://railstutorial.org/book/ruby-on-rails-tutorial). And of course I
have a newbish question.
Assume a contrived doc structure like:
<table class="navbar">
<tr>
<td class="status">moribund</td>
</tr>
</table>
Now lets say I want to write an RSpec controller test that will pass if
the status is "moribund" or "Moribund" or
"MORIBUND".
I know I can write:
it "should be moribund" do
get :show
response.should have_selector("td", :class => "status",
:content =>
"moribund")
end
... which captures the fact that the status string is inside a
"td.status" element, but is case sensitive. Alternatively I could
write:
it "should be moribund" do
get :show
response.body.should =~ /moribund/i
end
... which is case insensitive but doesn''t discriminate where the string
appears in the document.
What''s the right idiom to navigate to a specific place in a document
(preferably using CSS navigation syntax) AND perform a case-insensitive
test?
TIA.
- ff
P.S.: I fully appreciate that you shouldn''t normally hardwire the
document structure into the test itself -- that''s not what this
question
is about! :)
P.P.S: I know that response is an ActionController::TestResponse object,
but haven''t been able to find docs or sources for that -- where should
I
look?
--
Posted via http://www.ruby-forum.com/.
Evgeniy Dolzhenko
2011-Jan-25 06:08 UTC
[rspec-users] [newbie] CSS navigation in controller response?
You can pass a block to `have_selector` to nest your assertions, like:
response.should have_selector("td", :class => "status")
do |td|
td.to_s.should == /moribund/i # => td is [#<Nokogiri::XML::Element
...>, ...] here
end
On 1/25/2011 12:19 AM, Fearless Fool wrote:> I''m hooked on RSpec after my first taste (thanks to
> http://railstutorial.org/book/ruby-on-rails-tutorial). And of course I
> have a newbish question.
>
> Assume a contrived doc structure like:
>
> <table class="navbar">
> <tr>
> <td class="status">moribund</td>
> </tr>
> </table>
>
> Now lets say I want to write an RSpec controller test that will pass if
> the status is "moribund" or "Moribund" or
"MORIBUND".
>
> I know I can write:
>
> it "should be moribund" do
> get :show
> response.should have_selector("td", :class =>
"status", :content =>
> "moribund")
> end
>
> ... which captures the fact that the status string is inside a
> "td.status" element, but is case sensitive. Alternatively I
could
> write:
>
> it "should be moribund" do
> get :show
> response.body.should =~ /moribund/i
> end
>
> ... which is case insensitive but doesn''t discriminate where the
string
> appears in the document.
>
> What''s the right idiom to navigate to a specific place in a
document
> (preferably using CSS navigation syntax) AND perform a case-insensitive
> test?
>
> TIA.
>
> - ff
>
>
> P.S.: I fully appreciate that you shouldn''t normally hardwire the
> document structure into the test itself -- that''s not what this
question
> is about! :)
>
> P.P.S: I know that response is an ActionController::TestResponse object,
> but haven''t been able to find docs or sources for that -- where
should I
> look?
>
Fearless Fool
2011-Jan-25 09:10 UTC
[rspec-users] [newbie] CSS navigation in controller response?
Evgeniy Dolzhenko wrote in post #977322:> You can pass a block to `have_selector` to nest your assertions, like: > ... > td is [#<Nokogiri::XML::Element...>, ...] hereMost wonderfully cool. If it''s Nokogiri, then I''m on familiar turf. Thanks very much. - ff -- Posted via http://www.ruby-forum.com/.
Would replacing
:content => "moribund"
with
:content => /moribund/
work?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/rspec-users/attachments/20110125/207a1371/attachment.html>
Whoops, let me fix that:
Would replacing
:content => "moribund"
with
:content => /moribund/i
work?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/rspec-users/attachments/20110125/b9b9ffc5/attachment-0001.html>
Evgeniy Dolzhenko
2011-Jan-26 07:21 UTC
[rspec-users] [newbie] CSS navigation in controller response?
I wish it would, but no, it won''t due to the nature of how Webrat have_selector matcher works (it uses XPath queries behind the scenes) On 1/25/2011 11:18 PM, Nick wrote:> Whoops, let me fix that: > > Would replacing > :content => "moribund" > with > :content => /moribund/i > work? > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110126/62438c58/attachment.html>