测试rails控制器文件

时间:2013-06-13 11:35:51

标签: ruby-on-rails testing rspec controller

这是我在ParticipationConsent控制器中的创建操作。

ParticipationConsent#创建

 def create
    @participation_consent =   @project.participation_consent.new(params[:participation_consent])
    @participation_consent.user = current_user
    @participation_consent.confirmed = true
    @participation_consent.save!
 end

这是我对创建方法的测试

context 'get :create' do 
it 'should be success for approved creative' do 
  sign_in @approved_creative
  expect{
    get :create, :project => @project.id, :participation_consent => { :project_id => @project.id, :user_id => @approved_creative.id, :confirmed_in => false }
  }.to change(ParticipationConsent, :count).by(1)
  response.should redirect_to project_path(@project)
end

当我运行测试文件时,遇到此错误。

Failure/Error: get :create, :project => @project.id, :participation_consent => { :project_id => @project.id, :user_id => @approved_creative.id, :confirmed_in => false }
     ActionController::RoutingError:
       No route matches {:project=>1, :participation_consent => {:project_id=>1, :user_id=>2, :confirmed_in=>false}, :controller=>"participation_consents", :action=>"create"}

你知道我怎么处理它?<​​/ p>

编辑-1:

的routes.rb

 project_participation_consents POST   /projects/:project_id/participation_consents(.:format)                              {:action=>"create", :controller=>"participation_consents"}

1 个答案:

答案 0 :(得分:0)

问题是您是否正在发送创建操作的GET请求,这是一项POST操作,将get更改为post

此外,控制器代码中的@project来自何处?你应该从para中的id加载项目,大概是。

@project = Project.find(params[:project_id])