[rspec-users] AssertSelect for strings?

David Chelimsky dchelimsky at gmail.com
Sat Mar 10 08:56:37 EST 2007


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
> >
>


More information about the rspec-users mailing list