【问题标题】:How do I shorten the backtrace for a test failure in RSpec 2?如何缩短 RSpec 2 中测试失败的回溯?
【发布时间】:2011-02-19 04:00:00
【问题描述】:

当我的规格遇到错误时,我会收到如下消息:

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/ruby-1.9.2-p136@rails3tutorial/gems/rspec-expectations-2.3.0/lib/rspec/expectations/fail_with.rb:29:in `fail_with'
     # /home/kevin/.rvm/gems/ruby-1.9.2-p136@rails3tutorial/gems/rspec-expectations-2.3.0/lib/rspec/expectations/handler.rb:44:in `handle_matcher'
     # /home/kevin/.rvm/gems/ruby-1.9.2-p136@rails3tutorial/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'

我正在运行 Ruby 1.0.2、rails (3.0.3) 和 rspec (2.3.0)。 M .rspec 配置文件只指定了两个选项:

--drb --颜色

如何关闭扩展跟踪?

【问题讨论】:

    标签: rspec backtrace


    【解决方案1】:

    spec_helper.rb 中,您可以使用以下代码 sn-p 过滤回溯:

    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
    

    【讨论】:

      【解决方案2】:

      我更新了它以与 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
      

      这是纯金。谢谢卢卡索斯!

      【讨论】:

      • 为什么这对我不起作用?我知道代码加载,因为我在这个配置上方使用了puts 'HERE',我看到了,但我仍然得到超长的回溯。
      猜你喜欢
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      相关资源
      最近更新 更多