【发布时间】:2014-04-26 20:33:01
【问题描述】:
我是 Rails 的新手,试图测试 batiment 的创建以及此创建的 batiment 的 showpage 的重定向。
batiments_controller_test.rb
test "should create batiment" do
post :create, batiment: {nom: "New batiment"}
assert_response :success
assert_redirected_to assigns(:batiment)
end
batiments_controller.rb
def create
@batiment = Batiment.new(batiment_params)
if @batiment.save
flash[:notice] = "Bâtiment créé!"
redirect_to @batiment
else
render('new')
end
end
路线
POST /batiments(.:format) batiments#create
new_batiment GET /batiments/new(.:format) batiments#new
edit_batiment GET /batiments/:id/edit(.:format) batiments#edit
batiment GET /batiments/:id(.:format) batiments#show
运行测试时的终端:
1) Error:
BatimentsControllerTest#test_should_create_batiment:
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"batiments", :id=>nil} missing required keys: [:id]
test/controllers/batiments_controller_test.rb:25:in `block in <class:BatimentsControllerTest>'
【问题讨论】:
标签: ruby-on-rails unit-testing redirect testing ruby-on-rails-4