简体   繁体   English

rspec随机测试失败

[英]rspec tests randomly failing

CircleCI for testing, Ruby on Rails environment, using Redis for caching and postgres as the db. CircleCI用于测试,在Ruby on Rails环境中,使用Redis进行缓存,并将postgres用作数据库。 Rspec + cucumber for testing. Rspec +黄瓜进行测试。 I have tried everything but I still get failing tests, in quite a few different spec files, completely randomly. 我已经尝试了所有方法,但仍然在相当不同的规格文件中完全随机地通过了失败的测试。 Whenever I run the tests individually, they pass. 每当我单独运行测试时,它们就会通过。

This means that there's data left over from previous tests, or that some of my random FactoryGirl data is messing up sometimes. 这意味着以前测试遗留下来的数据,或者有时我的一些随机FactoryGirl数据弄乱了。 But, they always pass individually. 但是,它们总是单独通过。

First, I tried to fix tests manually, but I realized it was a bigger problem. 首先,我尝试手动修复测试,但是我意识到这是一个更大的问题。 Now, I'm trying to flush the db and Redis on each and every test, but even that doesn't work. 现在,我试图在每个测试中刷新db和Redis,但是即使这样也不起作用。

I have a flushall in my before/after each in spec_helper, which should apply to every single test. 在spec_helper中的每个操作之前/之后,我都有一个flushall,它适用于每个测试。 I know I shouldn't even need before AND after, but why not. 我知道我什至在前后都不需要,但是为什么不呢。

Then, I use database cleaner gem with the following settings: 然后,我使用具有以下设置的数据库清洁器gem:

RSpec.configure do |config|

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

end

What else could I be missing? 我还能缺少什么? I've been digging into this for a very long time now. 我已经研究了很长时间了。

Could it be that I'm using different Redis databases? 可能是我使用了不同的Redis数据库吗? I use this: 我用这个:

  Sidekiq.redis do |connection|
    connection.incr(company.message_count_key)
    connection.incr(company.all_time_message_count_key)
  end

But then I built a wrapper around Redis. 但是后来我围绕Redis构建了一个包装器。 called RED. 叫做RED。 and is that a different redis? 那是一个不同的redis吗? Would doing RED.flushall not be sufficient for wiping all REDIS? 做RED.flus是否足以擦拭所有REDIS?

You use different instances of redis in your app. 您在应用程序中使用不同的Redis实例。 In your initializers you initialize one redis, and then use a different redis to set the counters, however you read them from a different redis (RED) 在初始化程序中,您初始化一个Redis,然后使用其他Redis设置计数器,但是您从其他Redis(RED)中读取它们

RED =! 红色=! Sidekiq.redis, that's why it try to read a different redis. Sidekiq.redis,这就是为什么它尝试读取其他Redis的原因。

PS after my commit to split redis databases. 我承诺拆分redis数据库后的PS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM