[rspec-users] Spec for 'an array should not include an item'

Pat Maddox pergesu at gmail.com
Wed Oct 1 16:35:15 EDT 2008

Neil Cauldwell <lists at ruby-forum.com> writes:

> Pat Maddox wrote:
>> Neil Cauldwell <lists at ruby-forum.com> writes:
>>> (which it doesn't, therefore suggesting the spec is broken);
>>>    def do_get
>>>      ThinkingSphinx::Search.stub!(:search).and_return(users)
>>>      get :people, :search => "bob"
>>>    end
>>> ...so shouldn't the spec fail if we're returning all the users fixtures 
>>> when assigns[:results].should_not include(users)?
>>> Thanks
>> Well, flip the should_not to should and it ought to fail, and give you a
>> bit more info.  Or print the stuff out.  I imagine what's ultimately
>> happening is something along the lines of
>> [1,2,3].should_not include([1,2,3])
>> which is true.  [1,2,3] is not an element of the array [1,2,3].  I used
>> a simplified example here but it applies equally to an array of User
>> objects.
>> Anyway, what kind of debugging have you done to see what objects you're
>> actually dealing with?  I think doing "p assigns[:results]" and "p
>> users" will help point you in the right direction...
>> Pat
> Thanks Pat. That really helps. If I do the flip (please ignore the spec 
> description);
> it "should not include any inactive users in an assigned array of user 
> results " do
>   do_get
>   assigns[:results].should include(users)
> end
> Rspec tells me;
> should not include any inactive users in an assigned array of user 
> results
> expected [] to include []
> I was expecting :results to contain something, otherwise, wouldn't this 
> spec fail (?), or is nil different to empty?;
>    it "should assign an array of user results" do
>      do_get
>      assigns[:results].should_not be_nil
>    end
> ...so I'm one step closer but I'm now a little confused by the other 
> specs.

They are different.  This is similar to the example I posted before:
[].should include([])

that will fail...
[[]].should include([])

will pass.  Do you see the difference?  One of them is an empty array,
and one of them is an array containing a single element (which is itself
an empty array).

nil and [] are not equivalent in Ruby.

Anyway, I'm pretty convinced that your stubbing isn't working at all.
You can verify this by changing

If your stubbed method is being called then it'll raise an error, but I
suspect it's not.

As far as the fixtures stuff, I'm afraid I can't help much there.  I
think rails db fixtures are the devil and so have managed to put them
completely out of my consciousness.


More information about the rspec-users mailing list