# Functional Test has error when testing controller updates\_attribute of its parrent

**URL:** <https://www.ruby-forum.com/t/functional-test-has-error-when-testing-controller-updates-attribute-of-its-parrent/128201>\
**Category:** Rails\
**Created:** [February 5, 2008, 8:59pm UTC](https://www.ruby-forum.com/t/functional-test-has-error-when-testing-controller-updates-attribute-of-its-parrent/128201 "2008-02-05T20:59:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![NickCB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9de053/32.png) [@NickCB](https://www.ruby-forum.com/u/NickCB)\
**Post date:** [February 5, 2008, 8:59pm UTC](https://www.ruby-forum.com/t/functional-test-has-error-when-testing-controller-updates-attribute-of-its-parrent/128201/1 "2008-02-05T20:59:38Z")

</div>

I am working in Rails 2.0 and I have the following functional test.

class VotesControllerTest \< ActionController::TestCase  
def test\_should\_create\_vote  
assert\_difference(‘Vote.count’) do  
post :create, {:vote =\> {:vote\_value =\> 5,  
:user\_id =\> users(:nick).id,  
:entry\_id =\> entries(:BmxEntry).id}},  
:user\_id =\> users(:nick).id  
end  
assert\_redirected\_to vote\_path(assigns(:vote))  
end  
end

This tests the :create action of the vote controller which looks like  
this.  
def create  
@vote = Vote.new(params[:vote])

```
respond_to do |format|
  if @vote.save

    # Update the entry vote count and vote total columns
    @vote.entry.vote_count ||= 0
    @vote.entry.vote_total ||= 0
    @vote.entry.vote_count += 1
    @vote.entry.vote_total += @vote.vote_value
    @vote.entry.update_attribute(:vote_count,

```

@vote.entry.vote\_count)  
@vote.entry.update\_attribute(:vote\_total,  
@vote.entry.vote\_total)

```
    flash[:notice] = 'Vote was successfully created.'
    format.html { redirect_to(@vote)}
    format.xml { render :xml => @vote, :status

```

=\> :created, :location =\> @vote }  
else  
format.html { render :action =\> “new”, :layout =\> false }  
format.xml { render :xml =\> @vote.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

As you can see, after the vote is created the vote’s parent, entry, is  
updated to reflect the new vote. All the associations are set up where  
entry has\_many :votes and vote belongs\_to :entry. This code works just  
fine when working in development, but the test fails giving this  
error:

NoMethodError: You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.value

If I comment out the two following lines that update\_attribute then  
the test succeeds.

```
# @vote.entry.update_attribute(:vote_count,

```

@vote.entry.vote\_count)  
# @vote.entry.update\_attribute(:vote\_total,  
@vote.entry.vote\_total)

I am not sure what is causing this error, I checked and @vote.entry is  
set and entry attributes are available. Any ideas on what may be  
causing this.

---

<div class="post-metadata">

**Author:** ![NickCB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9de053/32.png) [@NickCB](https://www.ruby-forum.com/u/NickCB)\
**Post date:** [February 5, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/functional-test-has-error-when-testing-controller-updates-attribute-of-its-parrent/128201/2 "2008-02-05T21:21:34Z")

</div>

A little extra information,

Here is the end of the error message showing where I stopped:

c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action\_controller/  
test\_process.rb:15:in `process' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/ test_process.rb:393:in`process’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action\_controller/  
test\_process.rb:364:in `post' test/functional/votes_controller_test.rb:11:in`test\_should\_create\_vote’  
c:/ruby/lib/ruby/gems/1.8/gems/mocha-0.5.6/lib/mocha/  
test\_case\_adapter.rb:19:in ` __send__' c:/ruby/lib/ruby/gems/1.8/gems/mocha-0.5.6/lib/mocha/ test_case_adapter.rb:19:in`run’  
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active\_support/  
testing/default.rb:7:in `run’

And if I look at my test database after the test I see that a vote was  
created, and the entry was updated, just as I would like it too. So  
those calls are working but something about the @vote.entry.update  
causes the test to error.
