# Rspec problem stubbing with ActiveRecord assiociations

**URL:** <https://www.ruby-forum.com/t/rspec-problem-stubbing-with-activerecord-assiociations/149114>\
**Category:** Rails\
**Created:** [September 20, 2008, 8:52pm UTC](https://www.ruby-forum.com/t/rspec-problem-stubbing-with-activerecord-assiociations/149114 "2008-09-20T20:52:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![berlin\_2.0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/db5fbb/32.png) [@berlin\_2.0](https://www.ruby-forum.com/u/berlin_2.0)\
**Post date:** [September 20, 2008, 8:52pm UTC](https://www.ruby-forum.com/t/rspec-problem-stubbing-with-activerecord-assiociations/149114/1 "2008-09-20T20:52:58Z")

</div>

Hi everybody,

my name is Olaf, I’m living in Berlin and this is my first post. I’m  
quite new to rpsec, so please forgive my Newbieness! I’ve a problem  
stubbing a simple update action in a controller. I use a little hack  
to stub AR associations. So I added to my rspec\_helper.rb the  
following code:

* * *

# Stubbing Associations

module Spec  
module Mocks  
module Methods  
def stub\_association!(association\_name, methods\_to\_be\_stubbed =  
{})  
mock\_association =  
Spec::Mocks::Mock.new(association\_name.to\_s)  
methods\_to\_be\_stubbed.each do |method, return\_value|  
mock\_association.stub!(method).and\_return(return\_value)  
end  
self.stub!(association\_name).and\_return(mock\_association)  
end  
end  
end  
end

* * *

No I want to test the following action in my controller:

* * *

def update  
@profilecategory = @user.profilecategories.find(params[:id])  
respond\_to do |format|  
if @profilecategory.update\_attributes(params[:profilecategory])  
flash[:notice] = \_(‘Category was successfully updated.’)  
format.html { render :action =\> “index” }  
format.js  
else  
format.html { render :action =\> “edit” }  
format.js  
end  
end  
end

* * *

This is my Rspce code for doing so:

* * *

describe ProfilecategoriesController do  
before(:each) do  
@current\_user = mock\_model(User)  
@profilecategory = mock\_model(Profilecategory)  
@current\_user.stub\_association!(:profilecategories, :build =\>  
@profilecategory,  
:find =\> @profilecategory)  
@user = @current\_user  
controller.stub!(:current\_user).and\_return(@current\_user)  
end

describe “handling PUT /profilecategories/3 with successfull save” do  
it\_should\_behave\_like “the user is logged in”

```
before(:each) do
  @profilecategory.stub!(:update_attributes).and_return(true)
end

def do_put
  put :update, :id => '3', :profilecategory => {:name => 'new

```

name’}  
end

```
it "should be successfull" do
  do_put
  response.should be_success
end

it "should find the object" do

```

@user.profilecategories.should\_receive(:find).with(‘3’).and\_return(@profilecategory)  
do\_put  
end

```
it "should call the update_attributes function" do
  @profilecategory.should_receive(:uptdate_attributes).with({:name

```

=\> ‘new name’}).and\_return(true)  
do\_put  
end  
end  
end

* * *

When I run the rspec file, I always get this error:

* * *

should call the update\_attributes function  
Mock ‘Profilecategory\_1034’ expected :uptdate\_attributes with  
({:name=\>“new name”}) once, but received it 0 times

* * *

I don’t understand this. My Associations seem to work and when calling  
the update action in the browser, everything works fine. I guess it  
has something to do with AR not caching the associations. But I don’t  
know how to solve this problem.

I also thought about this approach, but didn’t get it to work  
properly:

* * *

## @profilecategories = mock(Profilecategory) @user.stub!(:profilecategories).and\_return(@profilecategories)

It would be great, if anyone could help me with that! I don’t know,  
what to do!

Best regards,  
Olaf
