RSpec 没有给我任何回溯

发布于 2024-12-19 18:54:25 字数 2037 浏览 1 评论 0原文

我尝试使用 rspec 进行回溯,但由于某种原因无法使其工作。

这是测试文件:

require 'spec_helper'

describe ActivityReport do
  it "should create a new instance given valid attributes" do
  activity = Factory(:activity_report)
end

这是我运行的命令:

rspec --backtrace spec/models/activity_report_spec.rb

这就是我得到的:

No examples matched {:focus=>true}. Running all.

ActivityReport
  should create a new instance given valid attributes (FAILED - 1)

Failures:

  1) ActivityReport should create a new instance given valid attributes
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /Users/pbartels/.rvm/gems/ruby-1.9.2-p290@brothelking/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:206

Finished in 40.76 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/models/activity_report_spec.rb:16 # ActivityReport should create a new instance given valid attributes

我的 .rspec:

--format nested
--color
--drb
--backtrace

以及我在spec_helper.rb 中的 RSpec 部分:

ENV["RAILS_ENV"] ||= 'test'

require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'factory_girl'
require 'database_cleaner'
require 'active_record/fixtures'

DatabaseCleaner.strategy = :truncation
DatabaseCleaner.start

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.mock_with :rspec

  config.use_transactional_fixtures = true

  config.treat_symbols_as_metadata_keys_with_true_values = true  
  config.filter_run :focus => true  
  config.run_all_when_everything_filtered = true

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

我尝试了使用和不使用“backtrace_clean_patterns”的情况。

有人知道这里出了什么问题吗?

I trying to get a backtrace with rspec but I can't get it working for some reason.

This is the test file:

require 'spec_helper'

describe ActivityReport do
  it "should create a new instance given valid attributes" do
  activity = Factory(:activity_report)
end

This is the command I run:

rspec --backtrace spec/models/activity_report_spec.rb

And this is what I get:

No examples matched {:focus=>true}. Running all.

ActivityReport
  should create a new instance given valid attributes (FAILED - 1)

Failures:

  1) ActivityReport should create a new instance given valid attributes
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /Users/pbartels/.rvm/gems/ruby-1.9.2-p290@brothelking/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:206

Finished in 40.76 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/models/activity_report_spec.rb:16 # ActivityReport should create a new instance given valid attributes

My .rspec:

--format nested
--color
--drb
--backtrace

And my RSpec part in spec_helper.rb:

ENV["RAILS_ENV"] ||= 'test'

require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'factory_girl'
require 'database_cleaner'
require 'active_record/fixtures'

DatabaseCleaner.strategy = :truncation
DatabaseCleaner.start

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.mock_with :rspec

  config.use_transactional_fixtures = true

  config.treat_symbols_as_metadata_keys_with_true_values = true  
  config.filter_run :focus => true  
  config.run_all_when_everything_filtered = true

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

I tried it with and without "backtrace_clean_patterns".

Anyone knows what's wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女净妖师 2024-12-26 18:54:25

没有回溯,因为失败的不是给定的代码行,它实际上是代码的结构。 Ruby 解释器实际上已经没有足够的空间来在堆栈上存储进一步的方法调用。

堆栈级别太深通常意味着您最近添加/修改了正在调用自身的代码,并进入了无限递归黑洞。查看您最近添加的代码(包括测试)以寻找线索。查看您是否从其内部调用方法。

例如,此代码将导致堆栈溢出:

def recursive_method
  recursive_method
end

您可能有一个方法调用或共享方法名称的字段,当您引用它时,它将进入无限循环/递归调用。

如果这没有为您指明正确的方向,您可能会被迫逐步恢复最近的代码更改,直到问题消失。当您回到代码中问题消失的状态时,您就会知道该问题与该更改有关,尽管具体细节可能不会立即清楚。

如果您确实没有任何进展(或者以智能方式恢复更改不可行),另一种选择是开始在您怀疑可能存在问题的代码中添加调试行。不知何故,您需要让您的应用程序写入日志文件或其他文件,以便您可以弄清楚它在死亡之前正在做什么。

There's no backtrace because it isn't a given line of code that's failing, it's actually the structure of your code. The ruby interpreter is literally running out of room to store further method calls on the stack.

Stack level too deep typically means that you've got recently added/modified code that is calling itself, and going into a infinitely recursive black hole. Look at the code that you've recently added (including tests) for the clue. See if you're calling a method from within itself.

For example, this code will cause stack overflow:

def recursive_method
  recursive_method
end

You've probably got a method call or field that shares the name of a method, and when you reference it, it's going into this infinite loop/recursive call.

If that doesn't point you in the right direction, you may be forced to incrementally revert you recent code changes until the problem goes away. When you get back to a state in your code where the problem goes away, you'll know that the issue has something to do with that change, though the specifics may not be immediately clear.

The other option, if you're really not getting anywhere with that (or it's not feasible to revert changes in an intelligent way), is to start adding debug lines to your code where you suspect the problem might be. Somehow you'll need to get your app to write to a log file or something so you can figure out what it is that it's doing right before it dies.

巾帼英雄 2024-12-26 18:54:25

对于任何来到这里寻找如何使用跟踪运行 rspec 的人来说,这是

rspec --backtrace 

For anyone arriving here looking for how to run rspec with trace, it's

rspec --backtrace 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文