2011-02-19 64 views
17

當我的規格打一個錯誤,我得到的消息是這樣的:如何縮短RSpec 2中測試失敗的回溯?

Vendor should reject duplicate names 
    Failure/Error: user_with_duplicate_email.should_not be_valid 
    expected valid? to return false, got true 
    # /home/kevin/.rvm/gems/[email protected]/gems/rspec-expectations-2.3.0/lib/rspec/expectations/fail_with.rb:29:in `fail_with' 
    # /home/kevin/.rvm/gems/[email protected]/gems/rspec-expectations-2.3.0/lib/rspec/expectations/handler.rb:44:in `handle_matcher' 
    # /home/kevin/.rvm/gems/[email protected]/gems/rspec-expectations-2.3.0/lib/rspec/expectations/extensions/kernel.rb:50:in `should_not' 
. 
. 
about 15 more lines 
. 
. 
    # /home/kevin/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/drb/drb.rb:1592:in `block (2 levels) in main_loop' 
    # /home/kevin/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/drb/drb.rb:1588:in `loop' 
    # /home/kevin/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/drb/drb.rb:1588:in `block in main_loop' 

我運行紅寶石1.0.2,軌(3.0.3),和RSpec(2.3.0)。中號.rspec配置文件只有兩個指定的選項:

--drb --colour

如何關閉擴展的痕跡?

回答

13

spec_helper.rb可以過濾回溯使用下面的代碼片段:

RSpec.configure do |config| 
    # RSpec automatically cleans stuff out of backtraces; 
    # sometimes this is annoying when trying to debug something e.g. a gem 

    # RSpec 3: 
    # config.backtrace_exclusion_patterns = [ 
    # RSpec 2: 
    config.backtrace_clean_patterns = [ 
    /\/lib\d*\/ruby\//, 
    /bin\//, 
    /gems/, 
    /spec\/spec_helper\.rb/, 
    /lib\/rspec\/(core|expectations|matchers|mocks)/ 
    ] 
end 
+0

在RSpec 3中試過,效果很好。 – Kris 2015-08-03 10:53:01

3

我更新它與Rspec的3.2.3工作。在spec_helper.rb放:

RSpec.configure do |config| 
    config.backtrace_exclusion_patterns = [ 
    /\/lib\d*\/ruby\//, 
    /bin\//, 
    /gems/, 
    /spec\/spec_helper\.rb/, 
    /lib\/rspec\/(core|expectations|matchers|mocks)/ 
    ] 
end 

這是純金。謝謝你,luacassus!

+0

爲什麼這不適合我?我知道代碼加載是因爲我在這個配置之上使用了'puts'HERE',並且我看到了它,但我仍然得到了超長的回溯。 – 2016-11-04 12:49:34