[rspec-devel] have_text not doing a contains?

David Chelimsky dchelimsky at gmail.com
Fri Mar 14 10:22:59 EDT 2008

On Fri, Mar 14, 2008 at 11:28 AM, Corey Haines <coreyhaines at gmail.com> wrote:
> We were struggling with a story yesterday, trying to be able to write
> something like
> Then user should see message 'Blah blah blah blah'
> Then("user should see message '$msg') do |expected_text|
>  response.should have_text(expected_text)
> end
> Our main problem was that the response was just a <p>Blah blah blah
> blah</p>. It just would not pass. I decided to look at the matcher for
> have_text, and this is what it is:
>         def matches?(response_or_text)
>           @actual = response_or_text.respond_to?(:body) ?
> response_or_text.body : response_or_text
>           return actual =~ expected if Regexp === expected
>           return actual == expected unless Regexp === expected
>          end
> Per the documentation,
> Use this instead of response.should have_tag() when you either don't know or
> don't care where on the page this text appears.
> So, my expectation was that this would do a String#include? or something
> similar, rather than ==. Or, even, creating a regexp with the expected and
> run it against actual.
> Does this make sense? If so, I'd like to put in a ticket and take this
> opportunity to learn how to submit a patch to rspec.
> Also, I'd probably switch the code into something like
> return (Regexp === expected) ? actual =~ expected :
> actual.include?(expected)
> But, of course, that's just personal style, as I think the if/unless in the
> last two lines is duplicate intention.

What you request makes great sense. But ....

The only problem with adding this is that existing examples that use a
String and expect an exact match (intentionally) would then give false
positives if the string changed to something that included that

My instinct is say "no go" here and improve the docs. Thoughts?

> Thoughts?
> -Corey
> --
> http://www.coreyhaines.com
> The Internet's Premiere source of information about Corey Haines
> _______________________________________________
>  rspec-devel mailing list
>  rspec-devel at rubyforge.org
>  http://rubyforge.org/mailman/listinfo/rspec-devel

More information about the rspec-devel mailing list