为什么我在rails minitest中收到URLGeneration错误?

时间:2015-07-03 21:04:50

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

我的测试用例:

  test 'creates a new post successfully' do
   get new_simulation_path
   assert_template 'simulations/new'
   assert_difference 'Simulation.count', 1 do
   post simulation_path, simulation: @simulation
  end
 end

我得到了:

 ActionController::UrlGenerationError:      
 ActionController::UrlGenerationError: No route matches   {:action=>"/simulations/new", :controller=>"simulations"}
        test/controllers/simulations_controller_test.rb:44:in `block in <class:SimulationsControllerTest>'
    test/controllers/simulations_controller_test.rb:44:in `block in <class:SimulationsControllerTest>'

为什么上述测试用例失败,而我能够从视图中创建新的Simulation(当我运行应用程序时)。 URL相同,但它不能用于编写测试

感谢您的帮助

修改

经过一些建议我把它变成了:

test 'creates a new post successfully' do
sign_in @user
get :new
assert_template 'simulations/new'
assert_difference 'Simulation.count', 1 do
  byebug
  post simulation_path, simulation: @simulation
end

差异现在我在No route matches {:action=>"show", :controller=>"simulations"}

中获得了post simulation_path, simulation: @simulation

1 个答案:

答案 0 :(得分:0)

这是在寻找simulations/new行动。

尝试:get :new代替get new_simulation_path

编辑 - 使用post的第二个错误:

post :create代替post simulation_path