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 te开发者_高级运维st 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
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.
You could so something like:
Length.should_receive(:get_length).exactly(n).times
More info: http://rspec.info/documentation/mocks/message_expectations.html
精彩评论