2016-05-15 71 views
2

我試圖在RSpec中使用Pry。 目標是能夠在方法中刪除綁定並對其進行調試。從RSpec調用的方法啓動時,Pry不起作用

這是我的。

的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 

如果我在規範文件中取消註釋結合並運行 rspec的 我進入撬起它的行爲如預期

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 spec中註釋binding.pry l,將其放入經過測試的方法中,並再次調用「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

不要驚訝我太多;我預計RSpec必須抓取stdout(和可能的stdin)才能測試期望值。還有另外一個很好的理由將輸出流等東西抽象出主線代碼。 –

+0

有趣。我改變的代碼是: 規格/ play_spec.rb '它 '應該修改變種' 做 預期(Play.hello(3))至EQ 4 end' 的lib/play.rb '高清。 self.hello(arg) var = 1 要求'pry'; binding.pry return var + arg end' 現在Pry正常工作。 但是,這種行爲的原因是什麼? 以及如何解決它?我需要檢查我的CLI應用程序是否打印正確的消息。 – Iezgen

+0

是的,你有多個對象正在同時讀/寫STDIN/STDOUT。結果,你在控制檯中變得奇怪。 –

回答

1

通過從測試中刪除stdout捕獲解決了問題。感謝大家。