# Spec for 'an array should not include an item'

**URL:** <https://www.ruby-forum.com/t/spec-for-an-array-should-not-include-an-item/149876>\
**Category:** RSpec\
**Created:** [September 30, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/spec-for-an-array-should-not-include-an-item/149876 "2008-09-30T19:35:06Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [September 30, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/spec-for-an-array-should-not-include-an-item/149876/1 "2008-09-30T19:35:06Z")

</div>

Does anyone know how I can write a spec along the lines of this (but a  
working implementation);

it “should not include inactive users” do  
do\_get  
assigns[:results].should\_not include(users(:inactive\_user))  
end

I’m a newbie so I’m not capable of explaining why it doesn’t work, but  
hopefully someone can set me straight.

Thanks

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [September 30, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/spec-for-an-array-should-not-include-an-item/149876/2 "2008-09-30T20:54:06Z")

</div>

Neil C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Thanks

What’s the error you’re getting? Because, assuming that stuff is hooked  
up properly, this ought to work fine.

Pat

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [September 30, 2008, 8:57pm UTC](https://www.ruby-forum.com/t/spec-for-an-array-should-not-include-an-item/149876/3 "2008-09-30T20:57:41Z")

</div>

Pat M. wrote:

> > 
> 
> * * *
> 
> rspec-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)

Also, what is your data? The matcher may not work as expected due to  
nuances in your data. For example, STI tends to mess things up in cases  
like this.

-Ben

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [October 1, 2008, 9:33am UTC](https://www.ruby-forum.com/t/spec-for-an-array-should-not-include-an-item/149876/4 "2008-10-01T09:33:21Z")

</div>

Ben M. wrote:

> Pat M. wrote:
> 
> > > 
> > 
> > * * *
> > 
> > rspec-users mailing list  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)
> 
> Also, what is your data? The matcher may not work as expected due to  
> nuances in your data. For example, STI tends to mess things up in cases  
> like this.
> 
> -Ben

Thanks guys. The problem is that it simply doesn’t fail when I expect it  
to. If I remove the call to the :inactive\_user fixture (i.e. I’m  
checking that the results should not include any of the ‘users’ at all),  
it still passes. This is the spec in full;

describe “handling GET /searches/people” do

def do\_get  
ThinkingSphinx::Search.stub!(:search).and\_return(users)  
get :people, :search =\> “bob”  
end

it “should be successful” do  
do\_get  
response.should be\_success  
end

it “should render people template” do  
do\_get  
response.should render\_template(‘people’)  
end

it “should assign an array of user results” do  
do\_get  
assigns[:results].should\_not be\_nil  
end

it "should not include any inactive users in an assigned array of user  
results " do  
do\_get  
assigns[:results].should\_not include(users(:inactive\_user))  
end

it "should not include any banned users in an assigned array of user  
results " do  
do\_get  
assigns[:results].should\_not include(users(:banned))  
end

end

Is my stubbing breaking this? I hope not because Thinking SPhinx isn’t  
determining the scope of the search in relation to the ‘active’ state of  
the users; the search is built on top of a named scope on the User  
model;

# GET /searches/people

# GET /searches/people.xml

def people  
@results = User.active.search params[:search], :page =\>  
params[:page], :per\_page =\> 15

```
respond_to do |format|
  format.html
  format.xml { render :xml => @results }
end

```

end

Cheers

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [October 1, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/spec-for-an-array-should-not-include-an-item/149876/5 "2008-10-01T15:35:17Z")

</div>

David C. wrote:

> On Wed, Oct 1, 2008 at 2:33 AM, Neil C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > > Also, what is your data? The matcher may not work as expected due to  
> > > describe “handling GET /searches/people” do
> > 
> > it "should not include any inactive users in an assigned array of user
> > 
> > @results = User.active.search params[:search], :page =\>  
> > params[:page], :per\_page =\> 15
> > 
> > respond\_to do |format|  
> > format.html  
> > format.xml { render :xml =\> @results }  
> > end  
> > end
> 
> Looks to me like this is testing the User here in addition to the  
> controller.
> 
> I’d recommend this:
> 
> @mock\_user = mock\_model(User)  
> User.stub!(:search\_active).and\_return([@mock\_user])
> 
> This would suggest a new method on User that wraps User.active.search,  
> which you can drive out with code examples directly on the model,  
> where the work is being done.
> 
> Then you could have only one example that deals w/ the collection of  
> users assigned for the view:
> 
> it “should expose @results for the view” do  
> do\_get  
> assigns[:results].should = [@mock\_user]  
> end
> 
> Then, in the model examples, you could either stub the Sphinx search  
> or just not use stubs/mocks at all.
> 
> HTH,  
> David

Thanks David. After a couple of reads through I don’t really grasp that.  
Where would this go in relation to the controller description I posted  
(?);

@mock\_user = mock\_model(User)  
User.stub!(:search\_active).and\_return([@mock\_user])

Would it work in the do\_get? Further to this, am I right in thinking  
that I would write a spec along these lines;

> it “should not expose @results for the view” do  
> do\_get  
> assigns[:results].should\_not == [@mock\_user]  
> end

Also (I’m assuming that the ‘model examples’ are the User model spec),  
the user\_spec.rb doesn’t have any specs that relate to search. Are you  
suggesting that there should be some in there, or, are all your  
suggestions all related to the controller example I posted? Please bear  
with me, I’m not much of a developer.

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [October 1, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/spec-for-an-array-should-not-include-an-item/149876/6 "2008-10-01T15:01:02Z")

</div>

On Wed, Oct 1, 2008 at 2:33 AM, Neil C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > Also, what is your data? The matcher may not work as expected due to  
> > describe “handling GET /searches/people” do
> 
> it "should not include any inactive users in an assigned array of user
> 
> @results = User.active.search params[:search], :page =\>  
> params[:page], :per\_page =\> 15
> 
> respond\_to do |format|  
> format.html  
> format.xml { render :xml =\> @results }  
> end  
> end

Looks to me like this is testing the User here in addition to the  
controller.

I’d recommend this:

@mock\_user = mock\_model(User)  
User.stub!(:search\_active).and\_return([@mock\_user])

This would suggest a new method on User that wraps User.active.search,  
which you can drive out with code examples directly on the model,  
where the work is being done.

Then you could have only one example that deals w/ the collection of  
users assigned for the view:

it “should expose @results for the view” do  
do\_get  
assigns[:results].should = [@mock\_user]  
end

Then, in the model examples, you could either stub the Sphinx search  
or just not use stubs/mocks at all.

HTH,  
David

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [October 1, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/spec-for-an-array-should-not-include-an-item/149876/7 "2008-10-01T17:08:59Z")

</div>

Pat M. wrote:

> Neil C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> > ```
> > respond_to do |format|
> > format.html
> > format.xml { render :xml => @results }
> > end
> > 
> > ```
> > 
> > end
> > 
> > Cheers
> 
> You’re stubbing User.search, but you don’t actually call it. search  
> ends up being called on a named\_scope collection proxy. So I think that  
> this is using the real implementation of both active and search.
> 
> I never saw you post a failure message, so I’m not actually sure what  
> problem you’re having. A failure message would be helpful.
> 
> Pat

Pat, I don’t see an error message but, I was expecting this to fail  
(which it doesn’t, therefore suggesting the spec is broken);

it "should not include any inactive users in an assigned array of user  
results " do  
do\_get  
assigns[:results].should\_not include(users)  
end

I was under the assumption that the above spec checks that we don’t have  
any of the users fixtures in the :results. The spec uses this;

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

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [October 1, 2008, 5:01pm UTC](https://www.ruby-forum.com/t/spec-for-an-array-should-not-include-an-item/149876/8 "2008-10-01T17:01:42Z")

</div>

Neil C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> ```
> respond_to do |format|
> format.html
> format.xml { render :xml => @results }
> end
> 
> ```
> 
> end
> 
> Cheers

You’re stubbing User.search, but you don’t actually call it. search  
ends up being called on a named\_scope collection proxy. So I think that  
this is using the real implementation of both active and search.

I never saw you post a failure message, so I’m not actually sure what  
problem you’re having. A failure message would be helpful.

Pat

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [October 1, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/spec-for-an-array-should-not-include-an-item/149876/9 "2008-10-01T18:11:06Z")

</div>

Neil C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [October 1, 2008, 10:41pm UTC](https://www.ruby-forum.com/t/spec-for-an-array-should-not-include-an-item/149876/10 "2008-10-01T22:41:34Z")

</div>

Neil C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > > …so shouldn’t the spec fail if we’re returning all the users fixtures  
> > > which is true. [1,2,3] is not an element of the array [1,2,3]. I used  
> > > description);  
> > > results  
> > > …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  
ThinkingSphinx::Search.stub!(:search).and\_return(users)  
to  
ThinkingSphinx::Search.stub!(:search).and\_raise(“boom!”)

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.

Pat

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [October 1, 2008, 9:17pm UTC](https://www.ruby-forum.com/t/spec-for-an-array-should-not-include-an-item/149876/11 "2008-10-01T21:17:30Z")

</div>

Pat M. wrote:

> Neil C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

I haven’t done any debugging, I haven’t come across any debugging  
techniques within Rspec before - please treat me as an absolute newbie  
to this.

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [October 1, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/spec-for-an-array-should-not-include-an-item/149876/12 "2008-10-01T23:08:45Z")

</div>

Pat M. wrote:

> Neil C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> > > > …so shouldn’t the spec fail if we’re returning all the users fixtures  
> > > > which is true. [1,2,3] is not an element of the array [1,2,3]. I used  
> > > > description);  
> > > > results  
> > > > …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  
> ThinkingSphinx::Search.stub!(:search).and\_return(users)  
> to  
> ThinkingSphinx::Search.stub!(:search).and\_raise(“boom!”)
> 
> 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.
> 
> Pat

I just tried the raise;

```
 ThinkingSphinx::Search.stub!(:search).and_raise("BOOM")

```

and;

RSpec Results  
36 examples, 5 failures  
Finished in 1.338815 seconds

SearchesController handling GET /searches/people  
should be successful  
BOOM

should render people template  
BOOM

I should have left my mac by now as my brain is pretty much fried by  
this of night, but, unless I’m mistaken, that means the stub is working,  
right?

I would like to move away from fixtures but it’ll have to wait for now.  
I just want to collect the users fixtures through the :people action in  
the searches controller and to make sure that a certain user fixture  
isn’t among the return users…I didn’t think it would so tricky!  
Thanks, though.

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [October 1, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/spec-for-an-array-should-not-include-an-item/149876/13 "2008-10-01T23:14:23Z")

</div>

Neil C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > They are different. This is similar to the example I posted before:
> > 
> > think rails db fixtures are the devil and so have managed to put them
> 
> I should have left my mac by now as my brain is pretty much fried by  
> this of night, but, unless I’m mistaken, that means the stub is working,  
> right?

Yes, so, that’s progress 🙂

Pat
