Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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功能测试功能单独工作,但不能一起工作。使用cancan、Desive、RoR4_Ruby On Rails_Ruby On Rails 4_Rspec_Devise_Cancan - Fatal编程技术网

Ruby on rails Rails功能测试功能单独工作,但不能一起工作。使用cancan、Desive、RoR4

Ruby on rails Rails功能测试功能单独工作,但不能一起工作。使用cancan、Desive、RoR4,ruby-on-rails,ruby-on-rails-4,rspec,devise,cancan,Ruby On Rails,Ruby On Rails 4,Rspec,Devise,Cancan,我正在使用RoR4、Cancan(1.5.0)和Desive(3.2.2)。 我正在使用Test:Unit测试我的应用程序。 问题是,如果我在不同的函数中分离请求,它会工作,但如果在一个函数中执行两个测试,它似乎会评估第一个请求的响应,即使在后续请求之后: 这项工作: test 'admin cannot delete product that has line_items associated' do sign_in @admin_user assert_no_difference('

我正在使用RoR4、Cancan(1.5.0)和Desive(3.2.2)。 我正在使用Test:Unit测试我的应用程序。 问题是,如果我在不同的函数中分离请求,它会工作,但如果在一个函数中执行两个测试,它似乎会评估第一个请求的响应,即使在后续请求之后: 这项工作:

test 'admin cannot delete product that has line_items associated' do
  sign_in @admin_user
  assert_no_difference('Product.count') do 
    delete :destroy, id: @prod_to_all
  end
  assert_redirected_to product_path(@prod_to_all)
end

test 'admin can delete product that has no line_items associated' do
  sign_in @admin_user
  assert_difference('Product.count', -1) do 
      delete :destroy, id: products(:prod_to_all_not_ordered)
  end
  assert_redirected_to products_path
end
如果我将这些请求放在一起,它将失败:

test 'admin cannot delete product that has line_items associated, but can delete one that has no line_items associated' do
  sign_in @admin_user
  assert_no_difference('Product.count') do 
    delete :destroy, id: @prod_to_all
  end
  assert_redirected_to product_path(@prod_to_all)

  assert_difference('Product.count', -1) do
    delete :destroy, id: products(:prod_to_all_not_ordered)
  end
  assert_redirected_to products_path
end
错误:

"Product.count" didn't change by -1.
  Expected: 5
  Actual: 6
我的问题是我有3个角色:公共用户、客户端和管理员。在不同的功能中为每个角色测试每个功能是一件痛苦的事情。即使对于简单的控制器,它也会变得比应该的更大,我讨厌这种解决方案。
你们有什么建议?我真的需要接受rspec及其上下文吗?还是可以不使用test:unit并保持代码干燥?
此外,在我看来,test:unit有些地方不太好,因为它没有正确地评估第二个请求……这是一个bug还是我不理解的更结构化的东西?
谢谢你

实际上我更喜欢单独的版本。它更干净。我个人喜欢我的测试不要太干。这使他们更加冗长。我更喜欢:

Rails功能测试意味着每个测试运行一个请求。如果要测试多个请求,可以使用集成测试。
在没有看到控制器代码的情况下很难判断错误。可能是您的应用程序在请求期间丢失了登录用户。或者它们共享状态(例如实例变量)。使用pry或其他调试器进行调试,或仅使用普通的旧puts来查看对象的状态。

谢谢米兰,我将按照您的建议进行调试。