[rspec-users] AssertSelect for strings?

Tilmann Singer tils-rspec at tils.net
Fri Mar 9 13:40:19 EST 2007


* 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


More information about the rspec-users mailing list