rspec测试隔离通过,但与其他测试一起运行时失败 [英] rspec test passes in isolation, but fails when run with other tests

查看:66
本文介绍了rspec测试隔离通过,但与其他测试一起运行时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些用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.

什么可能导致此行为?

更新:
我在与其他规格一起运行规格时遇到的错误(每次故障的错误都相同):

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

推荐答案

这通常表示您的其他规范在数据库中保留了一些与以后的工厂调用冲突的数据.我怀疑如果您调查工厂创建方法失败的原因,则可能会在客户的电子邮件中看到对唯一性的验证失败.

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆