Search code examples
ruby-on-railsactiverecordassociationsfunctional-testing

Rails belongs_to testing


I have a Proposal model that belongs to Project:

class Proposal < ActiveRecord::Base
  belongs_to :project
  has_many :articles, :as => :document, :dependent => :destroy
  has_many :sections, :through => :articles

  # proposal has project - test/unit/proposal_test.rb
  validates_presence_of :project_id
end

The route I set up to show this record is "http://domain.tld/projects/project-id/proposal", through this line in routes.rb - "map.resources :projects, :has_one => :proposal"

Now I want to test this through proposals_controller_test.rb.

  test "should show proposal" do
    get :show, :id => proposals(:one).to_param
    assert_response :success
  end

However, "rake test" keeps telling me this, and I have no idea how to fix it. Probably really easy for you guys, but new to me.

  1) Error:
test_should_show_proposal(ProposalsControllerTest):
ActiveRecord::RecordNotFound: Couldn't find Project without an ID
    app/controllers/proposals_controller.rb:18:in `show'
    /test/functional/proposals_controller_test.rb:34:in `test_should_show_proposal'

Here is my controller show logic:

  # should show proposal - test/functional/proposals_controller_test.rb
  def show
    @project = Project.find(params[:project_id])
    @proposal = @project.proposal

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @proposal }
    end
  end

A quick trip to "script/console" yields:

Loading development environment (Rails 2.3.4)
>> t = Project.first
=> #<Project id: 1, name: "Test">
>> t.proposal
=> #<Proposal id: 2, active: true, project_id: 1>

Whereas "script/console test" shows:

Loading test environment (Rails 2.3.4)
>> t = Project.first
=> #<Project id: 298486374, name: "Test">
>> t.proposal
=> nil

What is up with that wacky ID?!?! Please help me hook up associations in the test env!


Solution

  • Here is what was wrong.

    Automatic associations in ruby on rails fixtures

    http://ar.rubyonrails.org/classes/Fixtures.html

    • See the section on "Label references for associations (belongs_to, has_one, has_many)"

    Here is another optional explanation...


    Essentially, you have to monkey around with your fixtures some and remove the _id from foreign keys to get associations working (oddly enough). The articles explain everything. After I RTFM everything starting working perfectly. Just needed to know where to look. Don't forget to "rake db:test:prepare" & "rake test" first!