Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails在控制器创建方法中测试重定向_Ruby On Rails_Unit Testing_Redirect_Testing_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails Rails在控制器创建方法中测试重定向

Ruby on rails Rails在控制器创建方法中测试重定向,ruby-on-rails,unit-testing,redirect,testing,ruby-on-rails-4,Ruby On Rails,Unit Testing,Redirect,Testing,Ruby On Rails 4,我是Rails的新手,尝试测试batiment的创建,以及重定向到此创建的batiment页面的show bations\u controller\u test.rb test "should create batiment" do post :create, batiment: {nom: "New batiment"} assert_response :success assert_redirected_to assigns(:batiment) end def create @

我是Rails的新手,尝试测试
batiment
的创建,以及重定向到此创建的
batiment
页面的
show

bations\u controller\u test.rb

test "should create batiment" do
 post :create, batiment: {nom: "New batiment"}
 assert_response :success
 assert_redirected_to assigns(:batiment)
end
def create
  @batiment = Batiment.new(batiment_params)
 if @batiment.save
  flash[:notice] = "Bâtiment créé!"
  redirect_to @batiment
 else
  render('new')
 end
end
bations\u controller.rb

test "should create batiment" do
 post :create, batiment: {nom: "New batiment"}
 assert_response :success
 assert_redirected_to assigns(:batiment)
end
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>'
1)错误:
BatimentsControllerTest#test(测试)应(创建)Batiments:
ActionController::UrlGenerationError:没有路由匹配{:action=>“show”,:controller=>“batiments”,:id=>nil}缺少必需的键:[:id]
测试/控制器/batiments\u控制器\u测试。rb:25:in‘block in’

当然不能创建bation,因此该操作将视图呈现为“新建”。这解释了为什么
assert\u响应:success
通过(请求返回http代码200),以及为什么
assert\u重定向到分配(:bation)
失败(bation未保存,因此没有id)

检查保存失败的原因(可能是验证失败?检查
@batiment.errors
),然后按如下方式重写测试:

test 'should create batiment' do
 assert_difference('Batiment.count') do
   post :create, batiment: {nom: 'New batiment'}
 end
 assert_redirected_to assigns(:batiment)
end

你完全正确,这是一次验证失败。我觉得自己很愚蠢。多谢各位