问题描述
我当前收到一个类似以下错误:
I'm currently getting an error that looks like this:
NoMethodError: undefined method `debug' for nil:NilClass
/mnt/hgfs/Dropbox/Company/Project/lib/project/misc.rb:23:in `debug'
/mnt/hgfs/Dropbox/Company/Project/lib/project/validation/google_geocoding_validation_engine.rb:49:in `block in compare_addresses'
/mnt/hgfs/Dropbox/Company/Project/lib/project/validation/google_geocoding_validation_engine.rb:43:in `each'
/mnt/hgfs/Dropbox/Company/Project/lib/project/validation/google_geocoding_validation_engine.rb:43:in `compare_addresses'
/mnt/hgfs/Dropbox/Company/Project/lib/project/validation/google_geocoding_validation_engine.rb:32:in `valid?'
/mnt/hgfs/Dropbox/Company/Project/specs/project/validation/google_geocoding_validation_engine_spec.rb:56:in `block (2 levels) in <module:Validation>'
/home/tomas/ruby2/lib/ruby/2.0.0/minitest/unit.rb:1301:in `run'
我认为我会尝试使用 byebug 来找出为什么对象是nil
因为它永远都不应该是nil
.我将byebug
放在错误行的正上方:
I figured I'd try using byebug to figure out why the object is nil
since it's never supposed to be nil
. I placed byebug
just above the erroneous line:
def debug(&block)
if @logger.nil?
byebug
end
@logger.debug(@logger_name, &block)
end
并运行测试.我掉到了byebug调试界面,可以确认该对象确实是nil
.问题是我无法爬上堆栈:
And ran the tests. I was dropped to the byebug debugging interface and could confirm that the object was indeed nil
. The problem is that I can't climb up the stack:
(byebug) up
*** Adjusting would put us beyond the oldest (initial) frame.
(byebug) down
*** Adjusting would put us beyond the newest (innermost) frame.
(byebug) backtrace
--> #0 AddressKit::Misc::Logging.debug(block#Proc) at /mnt/hgfs/Dropbox/Kvantel/Address Kit/lib/addresskit/misc.rb:25
Warning: saved frames may be incomplete; compare with caller(0)
我为什么不能进入堆栈?这是byebug的问题,还是MiniTest的不兼容性?
Why can't I go up the stack? Is this an issue with byebug or perhaps an incompatibility with MiniTest?
推荐答案
Beebug的答案> = 1.5.0
在这种情况下打印并在调用栈中移动应该可以正常工作,而OP不会出现此问题.
Answer for byebug >= 1.5.0
Printing and moving around the callstack in situations like this one should just work and the OP wouldn't have this issue.
在调用Byebug.start
之前,Bybug不会开始跟踪调用堆栈信息,而byebug
命令在内部调用了Byebug.start
.因此,当您收到调试提示时,仅保存了一个调用堆栈帧,这就是您收到该消息的原因:
Byebug won't start tracking down callstack information until Byebug.start
is called, which is internally called by the byebug
command. So by the time you get the debugging prompt, only one callstack frame has been saved, that's why you get that message:
Warning: saved frames may be incomplete; compare with caller(0)
这就是为什么您不能移动up
或down
的原因:只有一帧.
and that's why you can't move up
or down
: there's only one frame.
要正确地浏览堆栈,您需要通过将Byebug.start
放到要开始跟踪调用堆栈信息的位置来将Byebug向上启动.例如,在google_geocoding_validation_engine_spec.rb
中的第56行之前.如果您需要完整的堆栈信息,则可以通过运行byebug可执行文件从一开始就运行byebug:
To properly navigate the stack, you need to start Byebug higher up, by dropping Byebug.start
wherever you want to start tracking down callstack info. For example, before line 56 in google_geocoding_validation_engine_spec.rb
. If you want full stack information, you can run byebug from the outset, by running the byebug executable:
byebug rake
或者您正在运行规范.
如果仍有问题,请告诉我!
If you still have issues, please let me know!
希望这会有所帮助.
这篇关于使用byebug进行调试时,如何扩展堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!