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

时间:2015-03-23 18:21:43

标签: ruby-on-rails rspec

我遇到了以随机种子顺序运行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)>'

2 个答案:

答案 0 :(得分: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 :(得分:1)

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

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

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