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

Neil Cauldwell lists at ruby-forum.com
Wed Oct 1 09:35:17 EDT 2008


David Chelimsky wrote:
> On Wed, Oct 1, 2008 at 2:33 AM, Neil Cauldwell <lists at ruby-forum.com> 
> 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.

-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list