如何在 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
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
$
答案 0 :(得分:5)
答案 1 :(得分: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 >
答案 2 :(得分:0)
您可能想查看binding.repl
。它可以在运行时以与Pry相同的方式启动IRB(和其他ruby控制台)。例如,您可以在rspec示例中说binding.repl.irb
而不是binding.pry
。
https://github.com/robgleeson/binding.repl
$ gem install binding.repl