Hi, Is there a way to use the have_tag matcher on a string instead of the response body from a view? I have a helper in a method rails project that returns an html fragment which I would like to test. Like this: module SomeHelper def some_helper_method "<p>foo</p>" end end context "The SomeHelper" helper_name :some specify do some_helper_method.should have_tag("p", "foo") end end Til ps: the new have_tag / with_tag stuff for testing views and rspec 0.8.2 in general rocks, thanks to all involved!
That''s a great idea, but AFAIK it is not supported by assert_select directly. Take a look at vendor/plugins/rspec_on_rails/spec/rails/matchers/assert_select_spec.rb. This is a copy of the assert_select_test from the assert_select_plugin, modified to fit into rspec. It sets up a controller that you can use to render whatever text you like, and set up specs like this: render_html %Q{ <p>foo</p> } response.should have_tag(''p'',''foo'') There''s no reason you shouldn''t be able to do this: render_html(some_helper_method) response.should have_tag(''p'',''foo'') It''s a bit sneaky, and perhaps too much work when you could just do: some_helper_method.should == "<p>foo</p>" Check it out and let me know if you have any questions. Cheers, David On 3/9/07, Tilmann Singer <tils-rspec at tils.net> wrote:> Hi, > > Is there a way to use the have_tag matcher on a string instead of the > response body from a view? I have a helper in a method rails project > that returns an html fragment which I would like to test. > > Like this: > > module SomeHelper > def some_helper_method > "<p>foo</p>" > end > end > > context "The SomeHelper" > helper_name :some > specify do > some_helper_method.should have_tag("p", "foo") > end > end > > > > Til > > > ps: the new have_tag / with_tag stuff for testing views and rspec > 0.8.2 in general rocks, thanks to all involved! > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On 3/9/07, Tilmann Singer <tils-rspec at tils.net> wrote:> ps: the new have_tag / with_tag stuff for testing views and rspec > 0.8.2 in general rocks, thanks to all involved!You''re welcome. Thanks for saying so. Cheers, David
* David Chelimsky <dchelimsky at gmail.com> [20070309 17:13]:> That''s a great idea, but AFAIK it is not supported by assert_select directly. > > Take a look at vendor/plugins/rspec_on_rails/spec/rails/matchers/assert_select_spec.rb. > This is a copy of the assert_select_test from the > assert_select_plugin, modified to fit into rspec. > > It sets up a controller that you can use to render whatever text you > like, and set up specs like this:This sets up a context for spec''ing controllers with :context_type => :controller. As far as I understand it I cannot use that when I want to test it from within a helper context, and I don''t know how to set up a controller inside a helper context (and if it would be feasible). I think you can pass a HTML::Node to assert_select somehow, so maybe there is a way, but so far I didn''t find it.> It''s a bit sneaky, and perhaps too much work when you could just do: > > some_helper_method.should == "<p>foo</p>"True. The actual reason why I want to do it is because the helper returns a string that can contain a link which I want to test independently. Here is a less contrived example: module SomeHelper def some_helper_method "<p>This sentence contains a <a href=''/foo''>link</a>.</p>" end end context "The SomeHelper" helper_name :some specify "should return a sentence" do some_helper_method.should have_tag("p", "This sentence contains a link.") end specify "should include a link" do some_helper_method.should have_tag("a", "link") end end It''s not too bad to workaround with regexps somehow like this: context "The SomeHelper" helper_name :some specify "should return a sentence" do some_helper_method.should match(/This sentence contains a.*link.*\./) end specify "should include a link" do some_helper_method.should match(%r{<a href=''/foo''>link</a>}) end end but using the assert_select syntax would be much more elegant. Til
On 3/9/07, Tilmann Singer <tils-rspec at tils.net> wrote:> * David Chelimsky <dchelimsky at gmail.com> [20070309 17:13]: > > That''s a great idea, but AFAIK it is not supported by assert_select directly. > > > > Take a look at vendor/plugins/rspec_on_rails/spec/rails/matchers/assert_select_spec.rb. > > This is a copy of the assert_select_test from the > > assert_select_plugin, modified to fit into rspec. > > > > It sets up a controller that you can use to render whatever text you > > like, and set up specs like this: > > This sets up a context for spec''ing controllers with :context_type => > :controller. As far as I understand it I cannot use that when I want > to test it from within a helper context, and I don''t know how to set > up a controller inside a helper context (and if it would be feasible). > > I think you can pass a HTML::Node to assert_select somehow, so maybe > there is a way, but so far I didn''t find it. > > > It''s a bit sneaky, and perhaps too much work when you could just do: > > > > some_helper_method.should == "<p>foo</p>" > > True. > > The actual reason why I want to do it is because the helper returns a > string that can contain a link which I want to test > independently. Here is a less contrived example: > > module SomeHelper > def some_helper_method > "<p>This sentence contains a <a href=''/foo''>link</a>.</p>" > end > end > > context "The SomeHelper" > helper_name :some > specify "should return a sentence" do > some_helper_method.should have_tag("p", "This sentence contains a link.") > end > > specify "should include a link" do > some_helper_method.should have_tag("a", "link") > end > end > > > It''s not too bad to workaround with regexps somehow like this: > > context "The SomeHelper" > helper_name :some > specify "should return a sentence" do > some_helper_method.should match(/This sentence contains a.*link.*\./) > end > > specify "should include a link" do > some_helper_method.should match(%r{<a href=''/foo''>link</a>}) > end > end > > > but using the assert_select syntax would be much more elegant.Agreed. I''d recommend you post a request to both rspec for have_tag and to rails for assert_select.> > > Til > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On 3/9/07, David Chelimsky <dchelimsky at gmail.com> wrote:> On 3/9/07, Tilmann Singer <tils-rspec at tils.net> wrote: > > * David Chelimsky <dchelimsky at gmail.com> [20070309 17:13]: > > > That''s a great idea, but AFAIK it is not supported by assert_select directly. > > > > > > Take a look at vendor/plugins/rspec_on_rails/spec/rails/matchers/assert_select_spec.rb. > > > This is a copy of the assert_select_test from the > > > assert_select_plugin, modified to fit into rspec. > > > > > > It sets up a controller that you can use to render whatever text you > > > like, and set up specs like this: > > > > This sets up a context for spec''ing controllers with :context_type => > > :controller. As far as I understand it I cannot use that when I want > > to test it from within a helper context, and I don''t know how to set > > up a controller inside a helper context (and if it would be feasible). > > > > I think you can pass a HTML::Node to assert_select somehow, so maybe > > there is a way, but so far I didn''t find it. > > > > > It''s a bit sneaky, and perhaps too much work when you could just do: > > > > > > some_helper_method.should == "<p>foo</p>" > > > > True. > > > > The actual reason why I want to do it is because the helper returns a > > string that can contain a link which I want to test > > independently. Here is a less contrived example: > > > > module SomeHelper > > def some_helper_method > > "<p>This sentence contains a <a href=''/foo''>link</a>.</p>" > > end > > end > > > > context "The SomeHelper" > > helper_name :some > > specify "should return a sentence" do > > some_helper_method.should have_tag("p", "This sentence contains a link.") > > end > > > > specify "should include a link" do > > some_helper_method.should have_tag("a", "link") > > end > > end > > > > > > It''s not too bad to workaround with regexps somehow like this: > > > > context "The SomeHelper" > > helper_name :some > > specify "should return a sentence" do > > some_helper_method.should match(/This sentence contains a.*link.*\./) > > end > > > > specify "should include a link" do > > some_helper_method.should match(%r{<a href=''/foo''>link</a>}) > > end > > end > > > > > > but using the assert_select syntax would be much more elegant. > > Agreed. I''d recommend you post a request to both rspec for have_tag > and to rails for assert_select.Well, curiosity got the better of me on this one. I went ahead and put in the feature request and implemented it: http://rubyforge.org/tracker/?func=detail&atid=3152&aid=9167&group_id=797 So it now works within rspec (trunk) as you requested. As it turns out, if you want to do it using assert_select, you can do this: node = HTML::Document.new(some_helper_method).root assert_select(root, "p", "foo") But it''s not quite as pretty as: some_helper_method.should have_tag("p","foo") Cheers, David> > > > > > > > Til > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > >
* David Chelimsky <dchelimsky at gmail.com> [20070310 15:30]:> > Agreed. I''d recommend you post a request to both rspec for have_tag > > and to rails for assert_select. > > Well, curiosity got the better of me on this one. I went ahead and put > in the feature request and implemented it: > > http://rubyforge.org/tracker/?func=detail&atid=3152&aid=9167&group_id=797That''s awesome, thanks a lot, and sorry for not replying earlier! Will switch our project now to run rspec and rspec_on_rails off trunk (which works great with piston btw.). Til