使用控制器的任何解决方案运行 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
注意:
答案 0 :(得分:0)
测试似乎假定应用程序处于一种状态,但是它可能是由于并发请求而更改的。 (我敢打赌,会话是共享的,在测试您的控制器时,您认为登录的人是X,但是是Y。)
确保每个并行运行都分开,似乎所有测试都连接到同一个数据库
在执行时连接了单个DB。 这在单线程应用程序中很有意义,因为在每个示例之后都会对其进行清理。但是,当并行运行时,可能会一团糟,并且您永远无法确定数据库中的内容。
您是否像这样更改了数据库配置:
test:
database: yourproject_test<%= ENV['TEST_ENV_NUMBER'] %>
如果您错过了-那就是您的