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

David Chelimsky dchelimsky at gmail.com
Wed Oct 1 09:00:23 EDT 2008


On Wed, Oct 1, 2008 at 2:33 AM, Neil Cauldwell <lists at ruby-forum.com> wrote:
> Ben Mabey wrote:
>> Pat Maddox wrote:
>>>>
>>> _______________________________________________
>>> rspec-users mailing list
>>> rspec-users at rubyforge.org
>>> 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

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

>
> Cheers
>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list