随机种子在rspec中失败但没有自行失败

问题描述 投票:3回答:2

我遇到了一个以随机种子顺序运行spec测试的问题。它在我自己进行测试时通过,这让我非常沮丧。我怎样才能解决这个问题?

describe MarketingInfo do
let(:question)       { create(:marketing_question) }
let(:answer)         { create(:marketing_answer, marketing_question:      question) }

let(:marketing_info) { MarketingInfo.new(create(:account)) }

describe '#create' do
let(:result) { marketing_info.create(info) }

context 'when valid' do
  let(:info) { { question.id => answer.id }  }
  specify { expect(result).to be_true }
end

context 'when invalid' do
  let(:info) { { question.id => '' } }
  specify { expect(result).to be_false }
end
end

 def initialize(answerable)
   @answerable = answerable
   @marketing_responses = []
 end

def create(response_data)
  response_data.each do |question_id, answer_array|
    m_response = build_marketing_response(question_id, answer_array)
    @marketing_responses << m_response if m_response
end

valid?
end  

以下是随机种子运行时的失败消息:

1) MarketingInfo#create when valid should be true
 Failure/Error: specify { expect(result).to be_true }
   expected: true value
        got: false
 # ./spec/form_objects/marketing_info_spec.rb:29:in `block (4 levels) in <top (required)>'
ruby-on-rails rspec
2个回答
2
投票

调试示例之间相互依赖性的另一个选择是RSpec Bisect。它将尝试隔离一组最低限度可重复的示例:

$ rspec -s 123 --bisect
Bisect started using options: "-s 123"
Running suite to find failures... (1 minute 4.16 seconds)
Starting bisect with 1 failing example and 600 non-failing examples.
Checking that failure(s) are order-dependent... failure appears to be order-dependent

Round 1: bisecting over non-failing examples 1-600 . ignoring examples 1-199 (22.55 seconds)
Round 2: bisecting over non-failing examples 200-400 .. ignoring examples 421-400 (28.87 seconds)
Round 3: bisecting over non-failing examples 300-350 .. multiple culprits detected - splitting candidates (37.26 seconds)
Round 4: bisecting over non-failing examples 330-335 .. multiple culprits detected - splitting candidates (43.32 seconds)
...
Bisect complete! Reduced necessary non-failing examples from 600 to 10 in 25 minutes 16 seconds.

The minimal reproduction command is:
  rspec './spec/controllers/etc_controller_spec.rb[1:1:1,1:1:2,1:2:1,1:3:1]' './spec/models/thing_spec.rb[1:1:2:1,1:1:2:2]' ... -s 123

喂养已知失败的种子可以加快速度。


1
投票

每当你运行RSPEC测试并且他们单独运行并通过但是作为一个组失败时,它可能意味着一些事情(我来学习经验)。有时它可能是套件有臭味并且物品的顺序是依赖的。但这听起来不像是问题。否则,这可能意味着数据库没有按照您预期的方式响应运行测试。

在任何情况下,我发现this blog post特别有助于调试这些情况(您可以检查specs.log文件以查看失败的测试,看看测试前发生了什么)。

也许你应该在每次测试运行后清除你的实例变量?

© www.soinside.com 2019 - 2024. All rights reserved.