[rspec-users] Rspec matchers confused by hashes?

Arne Brasseur arne.brasseur at gmail.com
Thu May 2 22:49:25 UTC 2013


This syntax should work

@test_hash["foobar"].size.should  eq(1)

But @test_hash["foobar"] evaluates to ["a","b","c"], so its size is
actually 3.
On May 3, 2013 12:12 AM, "Kwasi Mensah" <kwasi.mensah at gmail.com> wrote:

> sorry if this gets posted twice. I accidentally sent this before I was
> confirmed for the mailing list.
>
>
> On Thu, May 2, 2013 at 5:11 PM, Kwasi Mensah <kwasi.mensah at gmail.com>wrote:
>
>> I'm having trouble with one of my rspecs and it's coming down to the
>> tests being confused about what the subject is. Here's an example spec and
>> the corresponding test failures I get:
>>
>> describe "HashBug" do
>>   before do
>>     @test_hash = {"foobar" => ["a", "b", "c"]}
>>   end
>>   describe "Test fails" do
>>     it do
>>        @test_hash["foobar"].size should be 1
>>     end
>>   end
>>
>>   describe "Test passes" do
>>     it do
>>       @test_hash["foobar"][0].should eq "a"
>>     end
>>   end
>> end
>>
>> Failures:
>>
>>   1) HashBug Test fails
>>      Failure/Error: @test_hash["foobar"].size should be 1
>>
>>        expected #<Fixnum:3> => 1
>>             got #<String:70131453842100> => "Test fails"
>>
>>        Compared using equal?, which compares object identity,
>>        but expected and actual are not the same object. Use
>>        'actual.should eq(expected)' if you don't care about
>>        object identity in this example.
>>      # ./spec/requests/test_hash_bug_spec.rb:7:in `block (3 levels) in
>> <top (required)>'
>>
>> Finished in 0.00215 seconds
>> 2 examples, 1 failure
>>
>> Failed examples:
>>
>> rspec ./spec/requests/test_hash_bug_spec.rb:6 # HashBug Test fails
>>
>>
>> I'm not clear on why the match fails with one level of using [] but
>> passes with two levels.
>>
>> I'm using the command:
>> bundle exec rspec spec/requests/test_hash_bug_spec.rb
>>
>> I'm using the following rspec gems:
>> rspec (2.11.0)
>> rspec-core (2.11.1)
>> rspec-expectations (2.11.3)
>> rspec-mocks (2.11.3)
>> rspec-rails (2.11.0)
>>
>> And I can provide my full gem list if needed.
>>
>> Kwasi
>>
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20130503/90228bf2/attachment-0001.html>


More information about the rspec-users mailing list