gpt4 book ai didi

ruby-on-rails - Rspec parallel_test Controller 规范随机失败

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:36 25 4
gpt4 key购买 nike

在运行 parallel_rspec spec 时随机失败, Controller 有任何解决方案。

在 Gemfile 中

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

我的规范执行是这样的

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

注意:

  • 执行时连接单个数据库。
  • 除了 Controller ,一切正常。
  • 大多数像 json 相关条件都失败了。

最佳答案

看起来测试假定了应用程序的一种状态,但它可能被某些并发请求更改了。 (我敢打赌 session 是共享的,在测试您的 Controller 时,您认为登录的人是 X,但实际上是 Y)。

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

Single DB connected while execution. This makes sense in single threaded apps, because it's cleaned after each example. But when running in parallel it can be a mess, and you can never be sure what's in the DB.

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

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

如果你错过了,那就是你

关于ruby-on-rails - Rspec parallel_test Controller 规范随机失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56224351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com