【问题标题】:Rspec parallel_test controller spec random failsRspec parallel_test 控制器规范随机失败
【发布时间】:2019-05-20 15:50:42
【问题描述】:

在运行 parallel_rspec 规范时随机失败,控制器任何解决方案。

在 Gemfile 中

group :development, :test do 
  gem 'parallel_tests'  
end

我的 Spec 执行是这样的

name@root:/project# parallel_rspec spec/controllers/
4 processes for 21 specs, ~ 5 specs per process

Randomized with seed 3503

Randomized with seed 41377
 0/62 |>                                                       |  ETA: ??:??:?? 
Randomized with seed 43603
 0/50 |>                                                       |  ETA: ??:??:?? 
Randomized with seed 61169
 3/62 |==>                                                     |  ETA: 00:08:03 
  1) Admin::ReportingsController when signed-in non-admin GET #instance_reports redirects to root_url
     Failure/Error: expect( response ).to redirect_to root_url

       Expected response to be a redirect to <http://xxx/> but was a redirect to <http://yyyy>.
       Expected "http://xxxx/" to be === "http://xxxx/code".
     # ./spec/support/functions.rb:12:in `expect_root_redirect'
     # ./spec/controllers/admin/reportings_controller_spec.rb:25:in `block (4 levels) in <top (required)>'
     # ./spec/support/factory_bot.rb:18:in `block (3 levels) in <top (required)>'
     # /usr/local/bundle/gems/database_cleaner-1.7.0/lib/database_cleaner/generic/base.rb:16:in `cleaning'
     # /usr/local/bundle/gems/database_cleaner-1.7.0/lib/database_cleaner/base.rb:100:in `cleaning'
     # /usr/local/bundle/gems/database_cleaner-1.7.0/lib/database_cleaner/configuration.rb:86:in `block (2 levels) in cleaning'
     # /usr/local/bundle/gems/database_cleaner-1.7.0/lib/database_cleaner/configuration.rb:87:in `cleaning'
     # ./spec/support/factory_bot.rb:17:in `block (2 levels) in <top (required)>'

像这样随机失败。

如果我们使用以下命令运行,它可以正常工作。

rspec spec/controllers

注意:

  • 执行时连接单个 DB。
  • 除了控制器一切正常。
  • 大部分像json相关的条件都失败了。

【问题讨论】:

  • 您使用多个数据库吗?每个线程一个?
  • 你希望别人如何回答这个问题?我们需要能够重现问题(或至少看到足够多的代码来发现潜在错误)才能给出有意义的响应。
  • 请找到上面的输入,除此之外还需要什么!
  • 使用 1 个文件运行它工作正常,但不止一个它的填充像 ActiveRecord::DeadlockedJSON 错误响应代码不匹配 等.. 但是模型和其他测试用例使用 parallel_rspec 命令可以正常工作(单个文件和文件夹也是如此)如果我们使用以下命令运行它可以工作 > parallel_rspec -n 1 -t rspec规范/控制器,但单独的多进程不适用于控制器。
  • 一种可能是您的测试有副作用,请尝试运行bundle exec rspec --seed 61169 或将61169 替换为任何失败的种子编号。 Rspec 将尝试找到导致套件失败的最小集合,有可能一个测试设置了一个环境变量,而另一个测试意外使用了它。

标签: ruby-on-rails ruby testing rspec


【解决方案1】:

看起来测试假设了应用程序的一种状态,但它可能被一些并发请求改变了。 (我敢打赌,会话是共享的,在测试您的控制器时,您认为登录的人是 X,但它是 Y)。

确保每个并行运行都是分开的,看起来你的所有测试都连接到同一个数据库

执行时连接单个 DB。 这在单线程应用程序中是有意义的,因为在每个示例之后都会对其进行清理。但是当并行运行时,它可能会一团糟,你永远无法确定数据库中有什么。

您是否像这样更改了数据库配置:

test:
  database: yourproject_test<%= ENV['TEST_ENV_NUMBER'] %>

如果你错过了 - 那就是你

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 2012-11-16
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多