测试 Rails 4 控制器 [英] Testing Rails 4 Controller
问题描述
我在理解我的测试出了什么问题时遇到了一些麻烦,但是在测试控制器的更新方法时,我一直收到No Route Match".不过,通过浏览器提交表单是可行的.
I'm having some trouble understanding what my be wrong with my test, but I keep getting No Route Match when testing the update method for a controller. Submiting a form through a browser works, though.
我的路线文件:
namespace :merchant do
resources :users
get '/signup', to: "users#new"
end
我的控制器:
def update
respond_to do |format|
if @merchant_user.update(user_params)
format.html { redirect_to @merchant_user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'show' }
format.json { render json: @merchant_user.errors, status: :unprocessable_entity }
end
end
end
我的测试:
test "should update user" do
user = users(:jon)
user.first_name="Jonas"
put :update, :merchant_user =>user.attributes
assert_response :success
结束
结果:
1) Error:
Merchant::UsersControllerTest#test_should_update_user:
ActionController::UrlGenerationError: No route matches {:merchant_user=> {"id"=>"846114006", "email"=>"jon.sims@whatever.com", "first_name"=>"Jonas", "last_name"=>"Sims", "password_digest"=>"$2a$10$LVbV7pkd7li8sobYEauoS.4JVA2ZHzAXgPFbyiojYqgcDBHUE9bXW", "type"=>"Merchant::User", "created_at"=>"2013-07-11 22:59:41 UTC", "updated_at"=>"2013-07-11 22:59:41 UTC"}, :controller=>"merchant/users", :action=>"update"}
test/controllers/merchant/users_controller_test.rb:45:in `block in <class:UsersControllerTest>'
有什么提示吗?
推荐答案
从你的路由看起来好像需要一个 id
参数.
From the way your routes look it seems like it's expecting an id
parameter.
如果你从命令行执行rake routes
,它可能会显示一个看起来像
If you do rake routes
from the command line it will probably show a route that looks like
/merchant/users/:id/update
如果你像这样传入 id
put :update, id: user.id, Merchant_user: user.attributes
它应该可以工作.
If you pass in the id
like this put :update, id: user.id, merchant_user: user.attributes
it should work.
这篇关于测试 Rails 4 控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!