rspec 测试单独通过,但与其他测试一起运行时失败 [英] rspec test passes in isolation, but fails when run with other tests
问题描述
我有一些用 RSpec 编写的规范,用于测试各种模型.我使用 Factory Girl 生成对象进行测试.
I have some specs, written in RSpec, that test various models. I use Factory Girl to generate object for testing.
现在最奇怪的事情发生了:
当我运行 rspec spec/models/specific_model_spec.rb
--- 这通过了该规范中的所有测试
Now the most peculiar thing happens:
When i run rspec spec/models/specific_model_spec.rb
--- this passes all the tests in that spec
但是,当我运行 rspec spec/models
时 --- 本规范中的每个测试都失败,引用创建的无效关联(通过工厂)
However, when I run rspec spec/models
--- every test in this spec fails referring to an invalid association being created (via a factory)
工厂创建的关联显然是有效的,隔离运行测试也表明.
The association created by the factory is obviously valid as running the test in isolation also shows.
可能导致这种行为的原因是什么?
What could be causing this behavior?
更新:
将规范与其他规范一起运行时出现的错误(每次失败的错误都相同):
Update:
The error i get when running the spec together with other specs (the error is the same for each failure):
6) StreamItem adds a stream_item to a project and consultant when an engagement is added
Failure/Error: @project = Factory.create(:project, :name => 'bar' )
Validation failed: Customer is invalid
# ./spec/models/stream_item_spec.rb:44:in `block (2 levels) in <top (required)>'
project factory
在另一个规范中进行了测试并通过了良好的...
The project factory
is tested in another spec and passes fine...
更新 2:使用的相关工厂代码如下:
Update 2: The relevant factory code used is a follows:
Factory.define :manager, :class => User do |f|
f.sequence(:email) { |n| "bar#{n}@example.com" }
f.password "pass12"
f.sequence(:name) { |n| "Erwin#{n}" }
f.roles_mask 4
end
Factory.define :customer do |f|
f.sequence(:name) { |n| "foo customer#{n}" }
f.association :last_actor, :factory => :manager
f.account_id 1
end
Factory.define :project do |f|
f.sequence(:name) { |n| "foo project#{n}" }
f.association :manager, :factory => :manager
f.association :customer, :factory => :customer
f.start_date Date.today << 1
f.finish_date Date.today >> 2
f.status 1
f.association :last_actor, :factory => :manager
f.account_id 1
end
推荐答案
这通常表示您的其他规范在 DB 中留下了一些与后来的工厂调用冲突的数据.我怀疑如果您调查工厂创建方法失败的原因,您会看到唯一性验证失败,可能在客户的电子邮件中.
This usually indicates that your other specs leave some data in the DB that conflicts with later factory calls. I suspect if you look into why the factory create method failed, you'll see a validation for uniqueness fail, maybe on the customer's email.
关闭交易装置:
# spec_helper.rb
config.use_transactional_fixtures = false
and use database cleaner instead. This blog post might help as well.
这篇关于rspec 测试单独通过,但与其他测试一起运行时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!