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 >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20130502/c695eabb/attachment.html>
On Thu, May 2, 2013 at 3:01 PM, Kwasi Mensah <kwasi.mensah at 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20130502/b281e9b0/attachment.html>
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 On Thu, May 2, 2013 at 3:29 PM, Vighnesh Rege <vighnesh1987 at gmail.com>wrote:> > On Thu, May 2, 2013 at 3:01 PM, Kwasi Mensah <kwasi.mensah at 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 > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20130502/eec5a192/attachment.html>
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>
On Thu, May 2, 2013 at 6:29 PM, Vighnesh Rege <vighnesh1987 at gmail.com>wrote:> > On Thu, May 2, 2013 at 3:01 PM, Kwasi Mensah <kwasi.mensah at 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. >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 > > > _______________________________________________ > 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/20130502/ca9f7fa0/attachment.html>