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

pryを使用します。

シンプルで高速、構文の強調表示付き。インストール後、次のように書く必要があります。

binding.pry

コード内で実行を停止し、開発者デバッグコンソールを表示します

あなたがこじ開けに恋をした後、「pry-nav」と呼ばれる便利な宝石もあります

于 2012-09-11T16:40:27.250 に答える
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 に答える
0

あなたはチェックアウトしたいかもしれませんbinding.repl。Pryと同じように、実行時にIRB(およびその他のrubyコンソール)を起動できます。たとえば、 rspecの例binding.repl.irbの代わりに言うでしょう。binding.pry

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

于 2014-01-05T22:20:20.587 に答える