How to claim a mocha block block

This example is contrived, please do not take it verbatim as my code.

I need to state something like the following:

def mymethod
    Dir.chdir('/tmp') do
        `ls`
    end
end

In the end, I want to say that:

  • Dir.chdir is called with the appropriate parameters.
  • `is called with the appropriate parameters

I started with ...

Dir.expects(:chdir).with('/tmp')

but after that I am not sure how to call the block passed to Dir.chdir.

+5
source share
1 answer

You need to use the mocha yields method . In addition, it is quite interesting to record the expectations for the reverse method. You need to do the following:

expects("`")

? , .

, :

module MyMethod
  def self.mymethod
    Dir.chdir('/tmp') do
      `ls`
    end
  end
end

:

class MyMethodTest < Test::Unit::TestCase
  def test_my_method
    mock_block = mock
    mock_directory_contents = mock
    MyMethod.expects("`").with('ls').returns(mock_directory_contents)
    Dir.expects(:chdir).yields(mock_block).returns(mock_directory_contents)
    assert_equal mock_directory_contents, MyMethod.mymethod
  end
end

, , backtick. - self, . MyMethod, , mymethod, .

+3

All Articles