【发布时间】:2013-12-16 12:29:03
【问题描述】:
TL;DR:当 rspec 遇到错误时,如何停止中止所有规范?
在运行 rails 的内置测试套件时,我得到类似于以下的输出:
...F..F.EE...E
Finished in 0.64396 seconds
14 examples, 2 failures, 3 errors
. 代表通过测试,F 代表失败测试,E 代表错误测试。一个失败的测试意味着你的 Rails 应用程序的代码失败了,一个错误的测试意味着你的实际测试的代码失败了。错误的测试永远不会是好的。
不过,我喜欢的是,当程序遇到 E 时,它会继续执行所有其他测试。
我正在使用 rpsec,虽然我喜欢它,但我有点讨厌它遇到错误规范时如何完全放弃幽灵,并退出所有规范,如下所示:
12:39:37 - INFO - Running: spec
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1/gems/activerecord-4.0.1/lib/active_record/validations.rb:57:in `save!': Validation failed: Email has already been taken (ActiveRecord::RecordInvalid)
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1/gems/activerecord-4.0.1/lib/active_record/attribute_methods/dirty.rb:41:in `save!'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1/gems/activerecord-4.0.1/lib/active_record/transactions.rb:275:in `block in save!'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1/gems/activerecord-4.0.1/lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
虽然我确实喜欢错误报告,但我真的希望它在遇到错误规范时继续运行所有规范。而不是上面的输出,我真的想要以下内容:
12:40:01 - INFO - Running: spec
...F..F..E
Failures:
1) User A new user can be created email should == "awesomedog@hotmail.co.uk"
Failure/Error: its(:email) { should == "awesomedog@hotmail.co.uk" }
expected: "awesomedog@hotmail.co.uk"
got: "awesomedozg@hotmail.co.uk" (using ==)
# ./spec/model/user_spec.rb:11:in `block (3 levels) in <top (required)>'
2) User A new user can be created should not be valid
Failure/Error: it { should_not be_valid }
expected valid? to return false, got true
# ./spec/model/user_spec.rb:20:in `block (3 levels) in <top (required)>'
Errors:
1) `save!': Validation failed: Email has already been taken (ActiveRecord::RecordInvalid)
Finished in 0.64396 seconds
10 examples, 2 failures, 1 error
Failed examples:
rspec ./spec/model/user_spec.rb:11 # User A new user can be created email should == "awesomedog@hotmail.co.uk"
rspec ./spec/model/user_spec.rb:20 # User A new user can be created should not be valid
我在我的规范中使用 factory-girl 和 factory,并在每个规范之间使用数据库清理器来清理我的数据库。我正在使用 rspec-guard 在任何项目文件(tmp、log 或 db 中的文件除外)被保存的情况下运行所有规范。因为如果 rpsec 命中,它会不断消失一个错误,我收到了这个错误:
How can I clean my database between erroneous rspec specs?
基本上,database-cleaner 被配置为在规范开始和规范结束时清理我的数据库。因为 rspec 在遇到错误时会在规范中间退出,所以 database-cleaner 不会检测到规范已经完成,因此永远不会清理我的数据库。这意味着我必须使用我的数据库 shell 手动清空它。
我还希望查看我所有其他规格的状态,即使其中一个是错误的! Rspec 在这方面真的很傻我想!
这是我的工厂和规格:
spec/model/user_spec.rb:
require 'spec_helper'
describe User do
context "Valid user" do
user = FactoryGirl.create(:user_with_all_valid)
subject { user }
its(:first_name) { should == "Jimmy" }
its(:last_name) { should == "Thehat" }
its(:profile_name) { should == "Jimbohatboy893" }
its(:email) { should == "awesomedog@hotmail.co.uk" }
its(:password) { should == "thisisasupersecretpassword12234234" }
its(:password_confirmation) { should == "thisisasupersecretpassword12234234" }
end
end
spec/factories.rb:
FactoryGirl.define do
factory :user_with_all_valid, class: User do
first_name "Jimmy"
last_name "Thehat"
profile_name "Jimbohatboy893"
email "awesomedog@hotmail.co.uk"
password "thisisasupersecretpassword12234234"
password_confirmation "thisisasupersecretpassword12234234"
end
end
【问题讨论】:
-
您究竟是如何运行规范的?你在做
rspec spec还是rake spec?rake spec如果您不使用它可能会有所帮助。 -
好点,更新问题
-
您使用的是
stub!还是before :all?这些会因情况而异。 -
仍然不清楚您是如何运行测试的 - 通过警卫?我会在处理这个问题时禁用它。像 guard 和 spork 这样的工具偶尔会引起问题,以及为什么许多人停止使用它们。
-
对不起,更具体地说,我正在使用 rspec-guard。所以是的,所有的规范/测试都是由 rspec-guard 在文件保存时自动启动的。
标签: ruby-on-rails rspec