rubyunit-testingrspecmockingruby-mocha

Test method that was called from other method


I have module Database with method generate_from_database that spins for loops and calls method get_length. How can I test if get_length was called n times, by using rspec or mocha?

module Database
class Length < ActiveRecord::Base
  def get_length(i,j)
    ...
  end
end
def Database.generate_from_database
  ...
for i in 0...size
  for j in 0...size
    Length.new.get_length(i+1,j+1))
end
end

Solution

  • This:

    mock_length = mock("length")
    Length.should_receive(:new).exactly(n).times.and_return(mock_length)
    mock_length.should_receive(:get_length).exactly(n).times
    

    should work.