# Rspec fails when assigning to an ActiveRecord field

**URL:** <https://www.ruby-forum.com/t/rspec-fails-when-assigning-to-an-activerecord-field/151035>\
**Category:** Rails\
**Created:** [October 16, 2008, 6:49am UTC](https://www.ruby-forum.com/t/rspec-fails-when-assigning-to-an-activerecord-field/151035 "2008-10-16T06:49:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leogodin217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/67e7ee/32.png) [@leogodin217](https://www.ruby-forum.com/u/leogodin217)\
**Post date:** [October 16, 2008, 6:49am UTC](https://www.ruby-forum.com/t/rspec-fails-when-assigning-to-an-activerecord-field/151035/1 "2008-10-16T06:49:33Z")

</div>

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|
```

---

<div class="post-metadata">

**Author:** ![leogodin217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/67e7ee/32.png) [@leogodin217](https://www.ruby-forum.com/u/leogodin217)\
**Post date:** [October 20, 2008, 3:16am UTC](https://www.ruby-forum.com/t/rspec-fails-when-assigning-to-an-activerecord-field/151035/2 "2008-10-20T03:16:06Z")

</div>

On Wed, Oct 15, 2008 at 11:48 PM, leo [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![leogodin217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/67e7ee/32.png) [@leogodin217](https://www.ruby-forum.com/u/leogodin217)\
**Post date:** [October 21, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/rspec-fails-when-assigning-to-an-activerecord-field/151035/3 "2008-10-21T16:14:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![leogodin217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/67e7ee/32.png) [@leogodin217](https://www.ruby-forum.com/u/leogodin217)\
**Post date:** [October 25, 2008, 1:48am UTC](https://www.ruby-forum.com/t/rspec-fails-when-assigning-to-an-activerecord-field/151035/4 "2008-10-25T01:48:47Z")

</div>

On Tue, Oct 21, 2008 at 9:13 AM, leo [removed\_email\_address@domain.invalid](mailto: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/](http://rspec.info/documentation/mocks/)

Cheers,  
David
