为什么这个Rails assert_generates给出错误?

时间:2013-12-30 04:09:34

标签: ruby-on-rails ruby-on-rails-4

Rails 4.0.0。

测试用例

test "routing balance" do
  assert_generates '/users/1/balance', { controller: 'users', action: 'balance', id: '1' } 

错误

  1) Error:
UsersControllerTest#test_routing_balance:
ActionController::UrlGenerationError: No route matches {:controller=>"users", :action=>"balance", :id=>"1"}
    test/controllers/users_controller_test.rb:18:in `block in <class:UsersControllerTest>'

控制台

irb(main):229:0> app.user_balance_path 1
=> "/users/1/balance"

浏览器(Works)

http://localhost:3000/users/1/balance

的routes.rb

resources :users do
  get 'balance', to: :balance

耙路线

 user_balance GET    /users/:user_id/balance(.:format)           users#balance

其他测试(工程)

test "routing" do 
  assert_generates '/users/1', { controller: 'users', action: 'show', id: '1' }

参考

http://guides.rubyonrails.org/testing.html#rails-specific-assertions

1 个答案:

答案 0 :(得分:1)

尝试在测试中使用'user_id'而不是'id',因为这是您在路线中的内容