Functional Test has error when testing controller updates_attribute of its parrent

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
assert_redirected_to vote_path(assigns(:vote))

This tests the :create action of the vote controller which looks like
def create
@vote =[:vote])

respond_to do |format|

    # 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


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

=> :created, :location => @vote }
format.html { render :action => “new”, :layout => false }
format.xml { render :xml => @vote.errors, :status
=> :unprocessable_entity }

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

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.update_attribute(: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.

A little extra information,

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

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:inprocess’
test_process.rb:364:in post' test/functional/votes_controller_test.rb:11:intest_should_create_vote’
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:inrun’
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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs