我对rspec方法还相当陌生。我在redirect_to上读过很多帖子,但似乎无法通过这个错误。我试图在rspec测试中模拟表单变量/值的传递,但遇到了一个路由错误。我正在一个正在运行的应用程序上将测试转换为rspec测试。
在我的employees_microsoft_controller.rb中,我有一个更新方法,它需要params[:employees_micro][:id]。我的问题是如何在我的rspec测试中模拟它?
控制器:
def update
@em = nil
# check for id from form
if params[:employees_micro][:id]
@em = EmployeesMicro.find_by_id(params[:employees_micro][:id])
end
....
end
Rspec测试:注:###错误行###
# update and redirect
describe 'POST #update' do
it "updates employee_micro and redirect to employee_details" do
# @emp_micros set in before(each) above
@emp_micros.home_job = 123
# update method looks for params[:employees_micro][:id]
post :update, :employees_micro => { :id => @emp_micros } ### error line ###
expect(response).to redirect_to("employee_details_employee_path")
end
end
错误:
Failure/Error: post :update, :employees_micro => { :id => @emp_micros }
ActionController::RoutingError:
No route matches {:employees_micro=>{:id=>"11960"}, :controller=>"employees_micros", :action=>"update"}
我的>>帖子:更新行语法正确吗?
我不理解路由错误。我只是试图模拟将一个表单变量从测试中传递到update方法。如果我删除了post/put行之后的所有内容,我仍然会得到相同的错误,所以它肯定在那行上。
我也试过在测试中用"put"代替"post"。它让我犯了同样的错误。
谢谢你的任何提示或建议。
更新路由要求:id参数处于顶级,因此使用:
post :update, :id => @emp_micros, :employees_micro => { <other attributes to update> }