单独运行时单个规范通过,但在运行所有规范时失败 [英] individual spec passes when run alone, but fails when all specs are run

查看:40
本文介绍了单独运行时单个规范通过,但在运行所有规范时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 foo_controller_spec.rb 中有 30 个规范,当我使用规范运行整个文件时,出现 4 个失败和 2 个待处理.当我单独运行 4 个失败的规范时,其中 3 个仍然失败,但其中一个通过了.起初我认为这是一个数据库问题,在运行之间没有正确清除数据.所以我安装了 database_cleaner (http://github.com/bmabey/database_cleaner) 并将此代码添加到我的规范助手:

I have 30 specs in my foo_controller_spec.rb and when I run the entire file using spec, I get 4 failures and 2 pending. When I run the 4 failing specs individually, 3 of them still fail, but one of them passes. At first I thought it was a database issue, that the data wasn't being cleaned out properly between runs. So I installed database_cleaner (http://github.com/bmabey/database_cleaner) and added this code to my spec_helper:

config.before(:suite) do
  DatabaseCleaner.strategy = :truncation
  DatabaseCleaner.clean_with(:truncation)
end

config.before(:each) do
  DatabaseCleaner.start
  Sham.reset
  login
end

config.after(:each) do
  DatabaseCleaner.clean
end

现在,当我运行我的规范时,我可以看到表格在每个规范之间被截断,所以我知道它正在工作,但是当与所有其他规范一起运行时,规范仍然失败,但在单独运行时通过.我在这里错过了什么?

Now when I run my specs I can see that the tables are truncated between each spec so I know it's working, but the spec still fails when run with all the other specs but passes when run alone. What am I missing here?

推荐答案

我遇到了完全相同的问题,并且快疯了

I had exactly the same issue and was going out of my mind!

我跟踪了这​​样的问题(使用 Textmate):在项目抽屉中选择您的文件(多个文件),然后转到Bundles > Rspec > Run examples in selected files/directories".

I tracked the issue like this (with Textmate): select your files in project drawer (multiple files) and then go to "Bundles > Rspec > Run examples in selected files/directories".

诀窍是找出哪个文件对他人造成干扰.

The trick is to find which file is causing interference with others.

对我来说,是在一个文件中更改了 I18n.locale,导致所有其他示例的语言环境都发生了更改!

For me it was changing of I18n.locale in one file which caused the locale to be changed for all other examples!

我失去了几个小时的时间来疯狂...

I lost a few hours going nuts with this...

这篇关于单独运行时单个规范通过,但在运行所有规范时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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