我的测试用例:
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
答案 0 :(得分:0)
这是在寻找simulations/new
行动。
尝试:get :new
代替get new_simulation_path
编辑 - 使用post
的第二个错误:
post :create
代替post simulation_path