简体   繁体   English

我的其中一项测试由于使用let的某些问题而失败。 在rspec中

[英]One of my tests fails due to some issues with using let! in rspec

I have no clue what is wrong with the following: 我不知道以下问题是什么:

     let!(:account1) { create(:account) }

      describe ".active_between" do
        subject { Account.active_between(Date.today - 10.days, Date.today).to_a }
        it { is_expected.to eq([account1]) }
      end

Somehow this tests fails like this: 这种测试以某种方式失败了:

expected: [#<Account id: 2, lead_id: 2]
     got: [#<Account id: 2, lead_id: 3]

What could be causing the issue with the lead_id that's being changed? 是什么引起了被更改的lead_id问题? There's absolutely no code repsonsible for changing it. 绝对没有代码负责更改它。

The Factory Bot is set-up like this: 工厂机器人的设置如下:

  factory :account do
    association :lead
  end

The lead factory is defined as: 潜在客户工厂定义为:

  factory :lead do
    name 'Lead'
  end

Do you have any other tests that change account1 , and in particular it's lead in anyway? 您是否还有其他任何更改account1测试,特别是无论如何它都是lead的? Rspec will run tests in random order so it could be a test later on that is causing it. Rspec将以随机顺序运行测试,因此稍后可能是导致它的测试。

Regardless of that, these sorts of tests can be somewhat flaky. 无论如何,这些测试可能有些不稳定。 Do you really want to compare the entire object or just that the IDs match? 您是真的要比较整个对象还是ID匹配? If ID match is enough then your'e test is fine and you don't have to worry about other attributes (particularly timestamps being off a smidgen) 如果ID匹配足够,那么您的测试就可以了,并且您不必担心其他属性(尤其是时间戳不适合使用smidgen)

There is not enough information here unfortunately. 不幸的是,这里没有足够的信息。 Does it fail if you run ONLY this test? 如果仅运行此测试,它将失败吗?

If not, I would suggest using https://github.com/DatabaseCleaner/database_cleaner 如果没有,我建议使用https://github.com/DatabaseCleaner/database_cleaner

to solve the problem and ensure that each test starts with clean database. 解决问题并确保每个测试均从干净的数据库开始。

If that won't help, there must be something in models classes that interferes with it. 如果那没有帮助,则模型类中一定会有一些干扰它的东西。 If you encounter further problems, add also snippet from both models, we probably will be able to tell you more then 如果您遇到其他问题,请同时添加两个模型的代码段,然后我们可能会告诉您更多信息

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

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