Re: [rspec-users] Rspec matchers confused by hashes?

6 views
Skip to first unread message

Kwasi Mensah

unread,
May 2, 2013, 6:01:09 PM5/2/13
to rspec...@rubyforge.org
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....@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

Vighnesh Rege

unread,
May 2, 2013, 6:29:08 PM5/2/13
to rspec-users

On Thu, May 2, 2013 at 3:01 PM, Kwasi Mensah <kwasi....@gmail.com> wrote:
       @test_hash["foobar"].size should be 1

Shouldn't there be a dot between size and should?
Also, shouldn't the expected value be 3?

Even after making these changes:
```
@test_hash["foobar"].should be 3
```
the test fails for a reason that I'm not aware of. Personally, I'm more comfortable with the expect syntax[https://github.com/rspec/rspec-expectations]. I changed the first test to:

```
    it do
       expect(@test_hash["foobar"].size).to eq 3
    end
```

And it now works.


Vighnesh

Vighnesh Rege

unread,
May 2, 2013, 6:34:25 PM5/2/13
to rspec-users
I was wrong. This also works:
```
    it do
      @test_hash["foobar"].size.should be 3
    end
```

Looks like the error was due to the missing dot between size and should.

This test:
```
    it do
      should be 3
    end
```
gives the same error:
```
  1) HashBug Test fails
     Failure/Error: should be 3

       expected #<Fixnum:7> => 3
            got #<String:70269524470620> => "Test fails"

       Compared using equal?, which compares object identity,
       but expected and actual are not the same object. Use
       `expect(actual).to eq(expected)` if you don't care about
       object identity in this example.
     # ./eg_rspec.rb:7:in `block (3 levels) in <top (required)>'
```
It seems to indicate that if `should` is not called on an object, it takes the test description as the expected value - not entirely sure about this. Will have to read the docs to confirm.




Vighnesh

Arne Brasseur

unread,
May 2, 2013, 6:49:25 PM5/2/13
to rspec-users

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.

_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Kwasi Mensah

unread,
May 2, 2013, 7:02:17 PM5/2/13
to rspec-users
D'oh. You're right. writing "size.should" should work. And sorry for using the wrong number (was mixing with the original case that inspired this).  


Vighnesh

Reply all
Reply to author
Forward
0 new messages