0

これが以前に尋ねられた場合はお詫び申し上げます。

簡単なクラス:

class AskSO

  def initialize( filehandle )
    @filehandle   = filehandle
  end

  def library_start
    @filehandle << '<plist version="1.0">'
  end
end

Flexモックを使用した簡単な単体テスト

require 'rubygems'
require 'flexmock/test_unit'
require 'AskSO'
require 'test/unit'

class AskSOTest < Test::Unit::TestCase

  def setup
    @filehandle = flexmock( "filehandle", "<<" => "" )
  end

  def test_library_start
    @filehandle.should_receive( "<<" ).with( '<plist version="1.0">' ).once
    @AskSOInstance = AskSO.new( @filehandle )
    @AskSOInstance.library_start
  end
end

ruby AskSO-test.rb一緒に実行すると

  1) Failure:
test_library_start(AskSOTest)
    [/Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/validators.rb:40:in `validate'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/expectation.rb:123:in `flexmock_verify'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/expectation.rb:122:in `each'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/expectation.rb:122:in `flexmock_verify'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/expectation_director.rb:64:in `flexmock_verify'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/expectation_director.rb:63:in `each'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/expectation_director.rb:63:in `flexmock_verify'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/core.rb:76:in `flexmock_verify'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/core.rb:75:in `each'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/core.rb:75:in `flexmock_verify'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/core.rb:191:in `flexmock_wrap'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/core.rb:74:in `flexmock_verify'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/mock_container.rb:40:in `flexmock_verify'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/mock_container.rb:39:in `each'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/mock_container.rb:39:in `flexmock_verify'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/mock_container.rb:32:in `flexmock_teardown'
     /Library/Ruby/Gems/1.8/gems/flexmock-0.9.0/lib/flexmock/test_unit.rb:26:in `teardown']:
in mock 'filehandle': method '<<("<plist version=\"1.0\">")' called incorrect number of times.
<1> expected but was
<0>.

私は何が間違っているのですか?前もって感謝します

4

1 に答える 1

0

他の誰かが同じ問題に遭遇した場合に備えて、私自身の質問に答える-モックの定義を次のように変更しました

def setup
  @filehandle = flexmock( "filehandle" )
end

つまり、メソッドなし。同じ理由で、これは問題を解決します

于 2012-07-13T17:33:33.760 に答える