[rspec-users] Cucumber ".should contain(expected) does but fails anyway

David Chelimsky dchelimsky at gmail.com
Wed Apr 22 12:44:53 EDT 2009

On Wed, Apr 22, 2009 at 11:08 AM, James Byrne <lists at ruby-forum.com> wrote:
> David Chelimsky wrote:
>> Ruby. Try this in irb:
>> 'this'
>> => "this"
> This passes:
>  assert_equal (expected,
>                fx_doc.xpath('//rdf:RDF/xmlns:channel/xmlns:title').to_s,
>                "#{expected} not found")
> This does not:
>  fx_doc.xpath('//rdf:RDF/xmlns:channel/xmlns:title').to_s.should \
>                contain(expected)
> Or this:
>  fx_doc.xpath('//rdf:RDF/xmlns:channel/xmlns:title').to_s.should \
>                equal(expected)
> gives this lovely, and illogical error:
>      expected "<title>Bank of Canada: Noon Foreign Exchange
> Rates</title>"
>           got "<title>Bank of Canada: Noon Foreign Exchange
> Rates</title>"
>      (compared using equal?)
>       (Spec::Expectations::ExpectationNotMetError)
> Do not even start on how equal? in Ruby is defined completely at odds
> with common usage. I am just pointing out that this type of thing
> definitely violates the principal of least surprise.

Yeah - this is one of those decisions I made early on that I wish I
could take back, but doing so would break a lot of people's specs.
Maybe we should look into changing in it in rspec-2. The original
rspec had should_equal (equivalence) and should_be (object identity).

In the mean time, I'm not sure what better message we can give beyond
"compared using .equal?" without getting into a long treatise on
equality in Ruby, which seems out of place in a failure message.

> So, test unit it is.

The original issue you posted is with the contain matcher, which is in
webrat, not rspec. Why it's not working, I'm not quite sure, but if
you're going to throw out the baby with the bath water, you might
consider figuring out who the parents are :)

Looking at the webrat code for the contain matcher, it uses Nokogiri
under the hood. Not sure what's happening there, but you might try
have_tag instead:

should have_tag("title", "Bank of Canada: Noon Foreign Exchange Rates")

Let me know if it works.


More information about the rspec-users mailing list