2010-12-01 124 views
0

我有模塊數據庫,其中包含方法generate_from_database,用於爲循環旋轉並調用方法get_length。如何通過使用rspec或mocha來測試get_length是否被調用了n次?從其他方法調用的測試方法

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 

回答

0

此:

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

應該工作。

0

你可以這樣類似:

Length.should_receive(:get_length).exactly(n).times

更多信息:http://rspec.info/documentation/mocks/message_expectations.html

+0

我不喜歡的是: Length.should_receive(:get_length).exactly(10).times Database.generate_from_database 和我的失敗:預計10,收到0,但我知道,generate_from_database調用10次get_length。那麼爲什麼rspec沒有看到呼叫? – Mario 2010-12-01 14:10:32

相關問題