views:

166

answers:

2

Hi,
my Ruby On Rails unit-test fails in a simple string comparison and I can't figure out why.

In the model TestItem, I have

doc = REXML::Document.new(data)
@bugtitle = doc.root.get_text("/bugzilla/bug/short_desc")

where data is a xml-string returned by a Net::HTTP::post request. The data looks good, and if I output @bugtitle it contains the expected string. In my unit-test, I have

  test "bugtitle" do
    ti = testitems(:one)
    assert_equal("different RID folder for gating x-ray correction images",ti.bugtitle)
  end

Surprisingly (at least for me) the test fails with the following output (copied verbatim from the shell):

  1) Failure:
 test_bugtitle(TestTest) [unit/testitem_test.rb:7]:
 <"different RID folder for gating x-ray correction images"> expected but was
 <"different RID folder for gating x-ray correction images">.

I'm at a loss here as to where this error comes from since the string do look identical to me. For what it's worth, this is with rails 2.3.4 and ruby 1.8.6 on Windows (don't get me started).

+1  A: 

Those two seems identical, but you only see their representation after #inspect. You should check encoding and binary representation (if it's Ruby 1.9), maybe that's the problem.

MBO
Thanks for pointing me to #inspect, which didn't help in itself, but got me curious. See my answer for the solution.
jhwist
A: 

Since MBO poked me into the right direction, here is the solution:

For debugging, I added a p @bugtitle.class into the model code, which revealed, that @bugtitle was an REXML::Text instance. The correct model code is:

doc = REXML::Document.new(data)
@bugtitle = doc.root.get_text("/bugzilla/bug/short_desc").to_s

With that modification the unit-test completes without failures.

jhwist
You answered your own question, you can mark it as answered
MBO
Yes, in two days :)
jhwist