从RSpec test

时间:2016-05-15 12:06:18

标签: ruby rspec pry

我正在尝试将Pry与RSpec一起使用。 目标是能够在方法中删除绑定并对其进行调试。

这就是我所拥有的。

LIB / play.rb

class Play
  def self.hello
    print 'Hello world!'
    require 'pry'; binding.pry
  end
end

规格/ play_spec.rb

require_relative '../lib/play'

describe Play do
  it 'Play#hello should print message' do
    expect {Play.hello}.to output('Hello world!').to_stdout
    #require 'pry'; binding.pry
  end
end

如果我取消注释spec文件中的绑定并运行     rspec的 我正在进入Pry,其行为符合预期

Frame number: 0/23

From: /Users/iiezhachenko/Documents/Repos/bdd-ruby-      playground/spec/play_spec.rb @ line 6 :

    1: require_relative '../lib/play'
    2:
    3: describe Play do
    4:   it 'Play#hello should print message' do
    5:     expect {Play.hello}.to output('Hello world!').to_stdout
 => 6:     require 'pry'; binding.pry
    7:   end
    8: end

[1] pry(#<RSpec::ExampleGroups::Play>)> ls
RSpec::Core::MemoizedHelpers#methods: is_expected  should  should_not  subject
RSpec::Core::Pending#methods: pending  skip
RSpec::Mocks::ArgumentMatchers#methods:

如果我在spec填充中注释binding.pry,请将其放入测试的方法中并再次调用“rspec”。我正陷入完全混乱的Pry。

[1] pry(Play)> ls
[2] pry(Play)> fghjk
[3] pry(Play)> kweutyalfh 
[4] pry(Play)> exit
F

Failures:

  1) Play Play#hello should print message
     Failure/Error: expect {Play.hello}.to output('Hello world!').to_stdout

       expected block to output "Hello world!" to stdout, but output "Hello world!\n\e[1mFrame number:\e[0m 0/32\n\n\e[1mFrom:\e[0m /Users/iiezhachenko/Documents/Repos/bdd-ruby-playground/lib/play.rb @ line 4 Play.hello:\n\n    \e[1;34m2\e[0m: \e[32mdef\e[0m \e[1;36mself\e[0m.\e[1;34mhello\e[0m\n    \e[1;34m3\e[0m:   print \e[31m\e[1;31m'\e[0m\e[31mHello world!\e[1;31m'\e[0m\e[31m\e[0m\n => \e[1;34m4\e[0m:   require \e[31m\e[1;31m'\e[0m\e[31mpry\e[1;31m'\e[0m\e[31m\e[0m; binding.pry\n    \e[1;34m5\e[0m: \e[32mend\e[0m\n\n\e[0G\e[1m\e[1;34mPlay.methods\e[0m\e[0m: hello\n\e[1m\e[1;34mlocals\e[0m\e[0m: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_\nNameError: undefined local variable or method `fghjk' for Play:Class\nfrom (pry):1:in `hello'\nNameError: undefined local variable or method `kweutyalfh' for Play:Class\nfrom (pry):2:in `hello'\n"
       Diff:
       @@ -1,2 +1,17 @@
        Hello world!
       +Frame number: 0/32
       +
       +From: /Users/iiezhachenko/Documents/Repos/bdd-ruby-playground/lib/play.rb @ line 4 Play.hello:
       +
       +    2: def self.hello
       +    3:   print 'Hello world!'
       + => 4:   require 'pry'; binding.pry
       +    5: end
       +
Play.methods: hello
       +locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_
       +NameError: undefined local variable or method `fghjk' for Play:Class
       +from (pry):1:in `hello'
       +NameError: undefined local variable or method `kweutyalfh' for Play:Class
       +from (pry):2:in `hello'
     # ./spec/play_spec.rb:5:in `block (2 levels) in <top (required)>'

有人遇到过这样的问题吗?

2 个答案:

答案 0 :(得分:0)

通过从测试中删除stdout捕获来解决问题。谢谢大家。

答案 1 :(得分:0)

正如OP回答所指出的那样,该问题涉及控制台输出抑制,这可能会无意中破坏Pry。

如果您检查spec/spec_helper.rb文件,则RSpec配置块中可能有一些代码引用了$stderr和/或$stdout。删除这些代码行应该可以解决问题。

有关如何抑制此输出的其他示例,请参见以下问题的答案:Suppress console output during RSpec tests