我有一个属于项目的提案模型:
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
我设置显示这条记录的路线是“ http://domain.tld/projects/project-id/proposal ”,通过routes.rb中的这一行 - “map.resources :projects, :has_one => :proposal”
现在我想通过proposals_controller_test.rb 来测试一下。
test "should show proposal" do
get :show, :id => proposals(:one).to_param
assert_response :success
end
但是,“rake test”一直告诉我这个,我不知道如何解决它。对你们来说可能真的很容易,但对我来说是新的。
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'
这是我的控制器显示逻辑:
# 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
快速浏览“脚本/控制台”会产生:
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>
而“脚本/控制台测试”显示:
Loading test environment (Rails 2.3.4)
>> t = Project.first
=> #<Project id: 298486374, name: "Test">
>> t.proposal
=> nil
那个古怪的ID是怎么回事?!?!请帮我在测试环境中连接关联!