[rspec-users] Checking that a controller created a separate model object

O. Frabjous-Dey O.Frabjous.Dey at gmail.com
Mon Oct 13 17:14:17 EDT 2008


On Mon, Oct 13, 2008 at 1:47 PM, Nick Hoffman <nick at deadorange.com> wrote:

> On 2008-10-12, at 23:49, O. Frabjous-Dey wrote:
>
>> Hi Nick,
>>
>> The :new action comes straight from script/generate rspec_scaffold:
>>
>>  def new
>>    @group = Group.new
>>
>>    respond_to do |format|
>>      format.html
>>    end
>>  end
>>
>> I took out the XML rendering, but left in the respond_to block just in
>> case I wanted to add it or something else later.
>> And here's :create.
>>
>>  def create
>>    @group = Group.new(params[:group])
>>
>>    respond_to do |format|
>>      if @group.save
>>        flash[:notice] = 'Group was successfully created.'
>>        # Make this user an officer of the group
>>        Membership.create(:user_id => session[:user_id], :group_id => @
>> group.id, :rank => 'officer')
>>        format.html { redirect_to(@group) }
>>      else
>>        format.html { render :action => "new" }
>>      end
>>    end
>>  end
>>
>> Thanks!
>> O.
>>
>
> Hi again, O. In your spec, you're stubbing Group#new and returning a mock.
> As a result, the "create" action uses that mock when creating the Membership
> object. I've never specced relationships, so I'm not sure what to suggest.
> Hopefully someone else can give some advice.
>
> Cheers,
> Nick
>
> BTW, that last email of yours was sent directly to me, rather than to the
> mailing list. Let's keep all of the messages on the list.
>

Whoops!  Sorry this was sent straight to you, Nick.  I should have hit
Reply-all.  (Also that the subject line isn't formatted correctly; I
mistakenly thought that the listserv software would prepend [rspec-users] on
its own.)

I thought some more about the issue and I think I'm approaching the problem
the wrong way to begin with.  As I understand it, part of the philosophy of
RSpec is that using mocks and stubs when testing controllers and views
instead of touching the database helps to keep each test context
self-contained.  So I really ought to be checking to see if
Membership.create or or Membership.new is being called instead of examining
the model object's relationships themselves - not that I can anyway, since
it's a mock model.

Can anyone confirm if that sounds right?  If so, what method should I be
using?  #should_receive?  Thanks again in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081013/222c5c3e/attachment-0001.html>


More information about the rspec-users mailing list