# Rspec problem stubbing with ActiveRecord assiociations

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

<div class="post-metadata">

**Author:** ![slafochmed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@slafochmed](https://www.ruby-forum.com/u/slafochmed)\
**Post date:** [September 20, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/rspec-problem-stubbing-with-activerecord-assiociations/149121/1 "2008-09-20T23:20:08Z")

</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.

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

Best regards,  
Olaf

---

<div class="post-metadata">

**Author:** ![slafochmed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@slafochmed](https://www.ruby-forum.com/u/slafochmed)\
**Post date:** [September 21, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/rspec-problem-stubbing-with-activerecord-assiociations/149121/2 "2008-09-21T17:25:20Z")

</div>

Sorry for the double post!

Just a quick update on the problem:

What makes it even stranger:

## When I add this to my action:

## ]@profilecategory.testfunct()

## And then test the function call in the rspec like this:

describe “with successfull save” do  
before(:each) do  
@profilecategory.stub!(:update\_attributes).and\_return(true)  
@profilecategory.stub!(:testfunct).and\_return(true)  
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

```
  it "should call a test function" do
    @profilecategory.should_receive(:testfunct)
    do_put
  end
end

```

* * *

Everything works fine and it recognizes the testfunct call. So it just  
doesn’t recognize the update\_attributes call.

Any ideas?

On Sep 20, 8:52 pm, Olaf S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![slafochmed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@slafochmed](https://www.ruby-forum.com/u/slafochmed)\
**Post date:** [September 21, 2008, 10:13pm UTC](https://www.ruby-forum.com/t/rspec-problem-stubbing-with-activerecord-assiociations/149121/3 "2008-09-21T22:13:10Z")

</div>

I solved the problem by using save and partial updates in rails 2.1  
instead of update\_attributes. Very strange thing!

On Sep 20, 8:52 pm, Olaf S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![slafochmed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@slafochmed](https://www.ruby-forum.com/u/slafochmed)\
**Post date:** [September 22, 2008, 11:38am UTC](https://www.ruby-forum.com/t/rspec-problem-stubbing-with-activerecord-assiociations/149121/4 "2008-09-22T11:38:22Z")

</div>

On Sep 20, 7:52 pm, Olaf S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

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

Um, you spelt ‘:update\_attributes’ wrong?

Andrew
