Forum: Ruby on Rails rspec fails when assigning to an ActiveRecord field

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Leo G. (Guest)
on 2008-10-16 08:49
(Received via mailing list)
HI,
I have a controller action that assigns a user_id to an event record.
The app works fine from the browser.  However, rspec fails and I
cannot figure out why.  I have several tests failing but I'll give an
example created by script/generate rspec_controller.
Any help will be appreciated.




RSPEC test
102     describe "with invalid params" do
103
104       it "should expose a newly created but unsaved event as
@event" do
105         Event.stub!(:new).with({'these' =>
'params'}).and_return(mock_event(:save => false))
106         post :create, :event => {:these => 'params'}
107         assigns(:event).should equal(mock_event)
108       end


RSPEC Test result

     should expose a newly created but unsaved event as @event

    Mock 'Event_1039' received unexpected message :user_id= with (nil)

    /home/leo/railsprojects/trunk/memorymap/app/controllers/
events_controller.rb:44:in `create'
    ./spec/controllers/events_controller_spec.rb:106:

    42  def create
    43    @event = Event.new(params[:event])
    44    @event.user_id = session[:user_id]
    45
    46    respond_to do |format|
David C. (Guest)
on 2008-10-20 05:16
(Received via mailing list)
On Wed, Oct 15, 2008 at 11:48 PM, leo <removed_email_address@domain.invalid> 
wrote:
>
>
>
> RSPEC Test result
>
>     should expose a newly created but unsaved event as @event
>
>    Mock 'Event_1039' received unexpected message :user_id= with (nil)

This error message is telling you exactly what you need. In the
controller code (below), the #create action sends the #user_id=
message to @event, which is the mock_event defined in the code example
(above).

The mock_event method returns a mock object, which is designed to
complain when it receives messages you don't tell it to expect, and so
it complains when it receives #user_id=.

To eliminate that complaint, you can either set an explicit
expectation (if you think that is meaningful in your code example), or
you can tell it to ignore all unexpected messages by sending it
#as_null_object:

mock_event(:save => false).as_null_object

Cheers,
David
Leo G. (Guest)
on 2008-10-21 18:14
(Received via mailing list)
Thanks, I think I understand what you are saying.  When running the
tests, the @event in line 43 of the controller code is replaced with
the mock object.  Since it is a mock object, I need to tell the test
that the mock object can handle a :user_id message.  Can you tell me
how to do that?  I tried several ways but none of them seemed to work
and I couldn't find it in the documentation.  If anyone has a link to
info that would be awesome as I'd rather learn for myself if possible.
David C. (Guest)
on 2008-10-25 03:48
(Received via mailing list)
On Tue, Oct 21, 2008 at 9:13 AM, leo <removed_email_address@domain.invalid> 
wrote:
>
> Thanks, I think I understand what you are saying.  When running the
> tests, the @event in line 43 of the controller code is replaced with
> the mock object.  Since it is a mock object, I need to tell the test
> that the mock object can handle a :user_id message.  Can you tell me
> how to do that?  I tried several ways but none of them seemed to work
> and I couldn't find it in the documentation.  If anyone has a link to
> info that would be awesome as I'd rather learn for myself if possible.

http://rspec.info/documentation/mocks/

Cheers,
David
This topic is locked and can not be replied to.