Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Test for function_returning_nil.should have(x).items #141

Closed
wants to merge 1 commit into from

2 participants

@docwhat

nil.should have(8).items returns the expected output:
expected 8, got 0

You should get the same if you call it on a function that
returns nil. However, you get undefined method .items.

@docwhat docwhat Test for function_returning_nil.should have(x).items
`nil.should have(8).items` returns the expected output:
    expected 8, got 0

You should get the same if you call it on a function that
returns `nil`.  However, you get `undefined method .items`.
c620939
@justinko
Failures:

  1) have matcher should have(n).items 
     Failure/Error: nil.should have(8).items
     NoMethodError:
       undefined method `items' for nil:NilClass

I cannot reproduce what you're getting. Also "undefined method `items' for nil:NilClass" is the correct error message.

@justinko justinko closed this
@docwhat

@justinko That is the result I'm getting.

So is it a bug that nil.should have(8).items returns expected 8, got 0?

I find it confusing that it complains about .items -- it just seems to point at the wrong place. If expected 8, got 0 is wrong that maybe expected 8, got nil would be better.

Ciao!

@justinko

What version are you using?

Looks like your nil is not behaving correctly.

@docwhat

My bad, I should have written it down. I tested it with:

  • ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0]
  • ruby 1.9.2p318 (2012-02-14 revision 34678) [x86_64-darwin11.3.0]
  • ruby 1.8.7 (2012-02-08 patchlevel 358) [i686-darwin11.3.0]

Huh...okay, I cannot create the expected 8, got 0 error again and I don't know why. I tried it the two projects I've been working on the last few days.

I still think it'd be handy to have a better message, maybe Unable to get length of object 'nil' or something like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 20, 2012
  1. @docwhat

    Test for function_returning_nil.should have(x).items

    docwhat authored
    `nil.should have(8).items` returns the expected output:
        expected 8, got 0
    
    You should get the same if you call it on a function that
    returns `nil`.  However, you get `undefined method .items`.
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 0 deletions.
  1. +14 −0 features/built_in_matchers/have.feature
View
14 features/built_in_matchers/have.feature
@@ -101,3 +101,17 @@ Feature: have(n).items matcher
And the output should contain "expected at least 6 words, got 5"
And the output should contain "expected at most 4 words, got 5"
+ Scenario: have(n).items returns an intellegent error
+ Given a file named "nil_have_items_spec.rb" with:
+ """
+ def foo
+ nil
+ end
+
+ describe "foo" do
+ it { foo.should have(3).items }
+ end
+ """
+ When I run `rspec nil_have_items_spec.rb`
+ Then the output should contain "1 example, 1 failure"
+ And the output should contain "expected 3 items, got 0"
Something went wrong with that request. Please try again.