1

在我的rspec测试 运行期间如何启动irb ?

本质上,我希望能够玩各种东西,然后在程序的相应实例上运行我的rspec测试。我能够在程序的常规运行时成功地做到这一点,但是当我尝试在rspec测试运行时启动irb时遇到问题。

常规运行时间

test.rb

#!/usr/bin/env ruby
require 'irb'

puts 'hello world'
IRB.start
puts 'goodbye world'

运行

$ ./test.rb 
hello world
1.9.3-p194 :001 > puts 'yo'
yo
 => nil 
1.9.3-p194 :002 > exit
goodbye world

rspec运行时

test_spec.rb

require 'spec_helper'
require 'irb'

describe "irb" do
  it "should print 'hello world', launch irb, upon exiting irb print 'goodbye world'" do
    puts 'hello world'
    IRB.start
    puts 'goodbye world'
  end
end

运行

$ rake spec
/Users/username/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -S rspec ./spec/test_spec.rb
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}
hello world
1.9.3p194 :001 > require 'spec_helper'
 => false
1.9.3p194 :002 > require 'irb'
 => false
1.9.3p194 :003 >
1.9.3p194 :004 >   describe "irb" do
1.9.3p194 :005 >       it "should print 'hello world', launch irb, upon exiting irb print 'goodbye world'" do
1.9.3p194 :006 >           puts 'hello world'
1.9.3p194 :007?>         IRB.start
1.9.3p194 :008?>         puts 'goodbye world'
1.9.3p194 :009?>       end
1.9.3p194 :010?>   end1.9.3p194 :010?>
 => RSpec::Core::ExampleGroup::Nested_2
1.9.3p194 :010 >
goodbye world
.

Finished in 0.10321 seconds
1 example, 0 failures

Randomized with seed 62613
$
4

3 回答 3

6

使用

简单、快速、语法高亮。安装后,你只需要写:

binding.pry

在您的代码中停止执行并显示开发人员调试控制台

在你爱上 pry 之后,还有一个有用的 gem,叫做“pry-nav”

于 2012-09-11T16:40:27.250 回答
0

你可能想检查一下binding.repl。它可以像 Pry 一样在运行时启动 IRB(和其他 ruby​​ 控制台)。例如,您会说,binding.repl.irb而不是binding.pry在您的 rspec 示例中。

https://github.com/robgleeson/binding.repl
$ gem install binding.repl

于 2014-01-05T22:20:20.587 回答
0

启动调试器然后访问 IRB 可能会起作用。

require 'spec_helper'
require 'ruby-debug' # Install the gem first

describe "irb" do
  it "should print 'hello world', launch irb, upon exiting irb print 'goodbye world'" do
    puts 'hello world'
    debugger
    puts 'goodbye world'
  end
end

运行测试,它应该会命中调试器,你应该会看到一个 rdb 提示符。启动 IRB。

(rdb:1) irb
1.9.3p194 :001 >
于 2013-04-25T01:23:14.733 回答