Forum: RSpec Test HTML output from Rails helper

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E7bf2fbb32c8f54efba887aea3d2e46a?d=identicon&s=25 Thomas Watson Steen (Guest)
on 2008-10-18 11:06
(Received via mailing list)
Hi

I'm using RSpec in with Ruby on Rails. I've made a helper module
method that generates some HTML and would like to create a rspec test
to validate this HTML. I can of cause use regex to validate it as a
string, but would much rather validate it by traversing the DOM and
checking that the elements I expect is present. Any pointers?

/thomas
944f769c99deff7aa8bc3b5b93830b7a?d=identicon&s=25 Scott Taylor (Guest)
on 2008-10-18 11:51
(Received via mailing list)
On Oct 18, 2008, at 4:40 AM, Thomas Watson Steen wrote:

> Hi
>
> I'm using RSpec in with Ruby on Rails. I've made a helper module
> method that generates some HTML and would like to create a rspec
> test to validate this HTML. I can of cause use regex to validate it
> as a string, but would much rather validate it by traversing the DOM
> and checking that the elements I expect is present. Any pointers?

No - that's not really an option.  The rails stack has no idea about a
DOM.

One option, though, is to stub methods and use message expectations.
For instance, if you had a helper like the following:

module MyHelper
   def my_method(a_name)
     link_to(a_name, {:controller => "foo", :action => "foo"})
   end
end

You'd be able to write a spec like the following:

it "should link with the correct name" do
   helper.should_receive(:link_to).with("foo", {:controller =>
"foo", :action => "foo"})
   helper.my_method("foo")
end

Hope that helps,

Scott
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2008-10-18 15:16
(Received via mailing list)
Scott Taylor <mailing_lists@railsnewbie.com> writes:

> No - that's not really an option.  The rails stack has no idea about a
> DOM.

Hrm...he's just generating HTML, he should be able to use Hpricot.
Does have_tag work with plain strings, or only in examples where there's
a response body?  If it's the latter, we should make it work with plain
strings as well.  But Hpricot would certainly be useful I would think.


> You'd be able to write a spec like the following:
>
> it "should link with the correct name" do
>   helper.should_receive(:link_to).with("foo", {:controller =>  "foo",
> :action => "foo"})
>   helper.my_method("foo")
> end

I don't like this, I don't think you're testing anything here.

Pat
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick Hoffman (Guest)
on 2008-10-18 17:35
(Received via mailing list)
On 2008-10-18, at 09:16, Pat Maddox wrote:
>
> Pat

Hi Pat. I'm interested to hear how you'd spec that helper method,
because some of specs are similar to what Scott proposed, and I'm
always looking to improve them.

Cheers,
Nick
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2008-10-18 17:43
(Received via mailing list)
On Sat, Oct 18, 2008 at 6:16 AM, Pat Maddox <pergesu@gmail.com> wrote:


> Does have_tag work with plain strings
>

Yep, it does. That's how I spec #to_amcharts. :)

///ark
F86901feca747abbb5c6c020362ef2e7?d=identicon&s=25 Zach Dennis (zdennis)
on 2008-10-18 18:19
(Received via mailing list)
On Sat, Oct 18, 2008 at 11:34 AM, Nick Hoffman <nick@deadorange.com>
wrote:
>>> end
>>
>> I don't like this, I don't think you're testing anything here.
>>
>> Pat
>
> Hi Pat. I'm interested to hear how you'd spec that helper method, because
> some of specs are similar to what Scott proposed, and I'm always looking to
> improve them.


I'm pretty sure Pat is simply suggesting:

  it "should link with the correct name" do
     helper.my_method("foo").should have_tag("a[href=?]", the_path,
"foo")
  end

If my_method returns a string then have_tag will work. If my_method
returns an object that responds to "body" then it will also work (this
is how response.should have_tag('...') works as well,

--
Zach Dennis
http://www.continuousthinking.com
http://www.mutuallyhuman.com
944f769c99deff7aa8bc3b5b93830b7a?d=identicon&s=25 Scott Taylor (Guest)
on 2008-10-18 20:55
(Received via mailing list)
On Oct 18, 2008, at 9:16 AM, Pat Maddox wrote:

>>> and checking that the elements I expect is present. Any pointers?
> strings as well.  But Hpricot would certainly be useful I would think.
>

Yeah.  +1.  Not sure why I didn't think of that.

Scott
2ce9c0106b5851b2294ba5eb9f5c04bd?d=identicon&s=25 Ashley Moran (Guest)
on 2008-10-19 05:30
(Received via mailing list)
On Oct 18, 2008, at 10:50 am, Scott Taylor wrote:

> No - that's not really an option.  The rails stack has no idea about
> a DOM.
>
> One option, though, is to stub methods and use message
> expectations.  For instance, if you had a helper like the following:


Hi Scott

Another option would be Hpricot[1], which makes manipulating HTML
easier than CSV data.

Ashley

[1] http://code.whytheluckystiff.net/hpricot/

--
http://www.patchspace.co.uk/
http://aviewfromafar.net/
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2008-10-19 18:12
(Received via mailing list)
"Zach Dennis" <zach.dennis@gmail.com> writes:

>>>>  helper.my_method("foo")
>
> I'm pretty sure Pat is simply suggesting:
>
>   it "should link with the correct name" do
>      helper.my_method("foo").should have_tag("a[href=?]", the_path, "foo")
>   end

Yup

Pat
This topic is locked and can not be replied to.