在 rspec 中运行 ruby​​ 调试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5446594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:47:34  来源:igfitidea点击:

Running ruby debug in rspec?

ruby-on-railsrspecrakeruby-debug

提问by Allyl Isocyanate

I'm trying to get Ruby debugger running in one of my specs:

我正在尝试让 Ruby 调试器在我的规范之一中运行:

describe User do
  it "should be valid" do
    debugger
    User.new.should be_valid
  end
end

When I run rspec though, I get:

但是,当我运行 rspec 时,我得到:

debugger statement ignored, use -d or --debug option to enable debugging

I've tried the following:

我尝试了以下方法:

rake spec --debug
rake spec --debug  --trace
rake spec:models --debug
bundle exec rspec --debug
bundle exec rspec --debug spec/models/
bundle exec rspec --d spec/models/
bundle exec "rspec --debug" spec/models/
bundle exec rspec --debugger spec/models/
bundle exec --debugger rspec spec/models/
bundle --debugger exec rspec spec/models/
bundle --debugger exec rspec spec/models/
bundle exec --debugger rspec spec/models/
bundle exec rspec --debugger spec/models/

Any ideas on how to exec rspec in the right way? I'm on Rails 3.0.5, Ruby 1.9.2, RSpec 2.5.1, ruby-debug19.

关于如何以正确的方式执行 rspec 的任何想法?我在使用 Rails 3.0.5、Ruby 1.9.2、RSpec 2.5.1、ruby-debug19。

Thanks, Justin.

谢谢,贾斯汀。

采纳答案by Christopher Maujean

You will get what you want by including require 'ruby-debug'at the top of your spec:

通过包含require 'ruby-debug'在规范的顶部,您将获得所需的内容:

# spec/models/user_spec.rb
require 'spec_helper'
require 'ruby-debug'

describe User do
  it "should be valid" do
    debugger
    User.new.should be_valid
  end
end

You would then run rake specor rspecas normal

然后你会跑rake specrspec像往常一样

NOTE:I now prefer Ruby 2.0+ and pry. It is pretty much the same process:

注意:我现在更喜欢 Ruby 2.0+ 和 pry。这几乎是相同的过程:

require 'spec_helper'
require 'pry-debugger'

describe User do
  it "should be valid" do
    binding.pry
    expect(User.new).to be_valid
  end
end

Also, I generally put requires like this in my spec_helper file, so that pry-debugger is available to all of my specs.

此外,我通常将这样的需求放在我的 spec_helper 文件中,以便 pry-debugger 可用于我的所有规范。

回答by zetetic

You can create an .rspecconfiguration file in the root of your project and include the line:

您可以.rspec在项目的根目录中创建一个配置文件并包含以下行:

--debug

回答by Jarl

For Ruby >= 1.9.2

对于 Ruby >= 1.9.2

You should install the debuggergem instead ofruby-debug19. It you use bundler, you just put this in your Gemfile:

您应该安装调试器gem而不是ruby-debug19。如果你使用bundler,你只需将它放在你的 Gemfile 中:

group :test do
  gem "debugger"
end

After that you can just put

之后你可以把

rspec < 3.0

rspec < 3.0

--debug

rspec >= 3.0

rspec >= 3.0

-rdebugger

in your .rspecfile

在你的.rspec文件中

Then you can just run

然后你可以运行

bundle exec rake spec

without any additional arguments. There is no need to modify your source codeeither (not even your test source code)

没有任何额外的参数。有没有必要修改源代码是(甚至不是你的测试源代码)

回答by drinor

For ruby 2.0 I use byebug: https://github.com/deivid-rodriguez/byebug

对于 ruby​​ 2.0,我使用 byebug:https: //github.com/deivid-rodriguez/byebug

gem 'byebug'

Code:

代码:

# spec/models/user_spec.rb
require 'spec_helper'
require 'byebug'

describe User do
  it "should be valid" do
    byebug
    User.new.should be_valid
  end
end

回答by Ode

The best way I have found to debug in rSpec is by adding the following to your 'spec_helper.rb' file

我发现在 rSpec 中调试的最好方法是将以下内容添加到您的“spec_helper.rb”文件中

def logger
  Rails.logger
end

You can then access all the logger methods within your rSpec files and incorporate such things as tagged logging. This of course is for Rails 3 and up. If you have anything prior to Rails 3 then add this instead:

然后,您可以访问 rSpec 文件中的所有记录器方法并合并诸如标记记录之类的东西。这当然适用于 Rails 3 及更高版本。如果您在 Rails 3 之前有任何内容,请添加以下内容:

def logger
  RAILS_DEFAULT_LOGGER
end

Once you have your logging statements in place you can enter

准备好日志记录后,您可以输入

tail -f log/test.log

in your terminal shell in order to watch your logging statements while the tests are run.

在您的终端 shell 中,以便在测试运行时查看您的日志记录语句。

Of course in your actual rspec test you would enter something such as

当然,在您的实际 rspec 测试中,您会输入诸如

logger.debug "#{1.class}"  # => Fixnum

If you want to filter your debug statements from the rest of your test log simply prepend a random string on to your debug statement and pipe the output of the tail command to grep.

如果您想从测试日志的其余部分过滤调试语句,只需在调试语句中添加一个随机字符串,并将 tail 命令的输出通过管道传送到 grep。

Example:

例子:

logger.debug "random_string #{1.class}"   # => Fixnum

tail -f log/test.log | grep random_string

Update

更新

I've changed my opinion on this. You should install pry, pry-doc, and pry-debug, pry-debugger, and pry-rails. Then use binding.pry in your code to open an interactive debugger console that rules the world!

我已经改变了我对这件事的看法。您应该安装 pry、pry-doc 和 pry-debug、pry-debugger 和 pry-rails。然后在您的代码中使用 binding.pry 打开一个统治世界的交互式调试器控制台!

回答by Ryan Taylor

The best and cleanest option is to use --requirein your .rspecfile. What you put depends on which gem you use for debugging.

最好和最干净的选择是--require在您的.rspec文件中使用。您放置的内容取决于您用于调试的 gem。

--color
--require pry
--require rails_helper

These correspond to command line options (-d or --debug is now deprecated).

这些对应于命令行选项(-d 或 --debug 现在已弃用)。

Feel free to use debugger, ruby-debugor pry(pry-rails in your Gemfile).

随意使用debugger,ruby-debugpry(在你的 Gemfile 中撬轨)。

For your Gemfile:

对于您的 Gemfile:

group :test, :development do
  gem 'pry-rails'
end

Putting require 'ruby-debug'etc. at the top of your spec is simply more tightly coupled -- especially since here the top voted comment suggests putting it individually in ALL your files. With the new .rspecfile you shouldn't need to put require 'spec_helper'or require 'rails_helper'at the top of your files anymore.

require 'ruby-debug'etc. 放在规范的顶部只是更紧密地耦合 - 特别是因为在这里投票最高的评论建议将它单独放在所有文件中。使用新.rspec文件,您不再需要将require 'spec_helper'require 'rails_helper'放在文件的顶部。

They make more sense as implicit command line arguments.

它们作为隐式命令行参数更有意义。