据说,如果对mock stub 和fakes概念不熟悉的话可以看看
这个 我自己看了一下,没懂。还是看例子容易些
例如,有这么个东西需要测试
class MailPurge
def initialize(imap)
@imap = imap
end
def purge(date)
# IMAP wants dates in the format: 8-Aug-2002
formatted_date = date.strftime('%d-%b-%Y')
@imap.authenticate('LOGIN', 'user', 'password')
@imap.select('INBOX')
message_ids = @imap.search(["BEFORE #{formatted_date}"])
@imap.store(message_ids, "+FLAGS", [:Deleted])
end
end
我们的mock这样用
#相当于mock出一个imap对象有authenticate select等方法,重要是就一个ids是返回值有依赖
def test_purging_mail
date = Date.new(2010,1,1)
formatted_date = '01-Jan-2010'
ids = [4,5,6]
mock = MiniTest::Mock.new
# mock expects:
# method return arguments
#-------------------------------------------------------------
mock.expect(:authenticate, nil, ['LOGIN', 'user', 'password'])
mock.expect(:select, nil, ['INBOX'])
mock.expect(:search, ids, [["BEFORE #{formatted_date}"]])
mock.expect(:store, nil, [ids, "+FLAGS", [:Deleted]])
mp = MailPurge.new(mock)
mp.purge(date)
assert mock.verify
end
如果,嫌不够的话,可以学一下mock的
源代码
def initialize
@expected_calls = {}
@actual_calls = Hash.new {|h,k| h[k] = [] }
end
#用来记录实际调用的,后面用到
def expect(name, retval, args=[])
n, r, a = name, retval, args # for the closure below
@expected_calls[name] = { :retval => retval, :args => args }
self.class.__send__(:define_method, name) { |*x|
raise ArgumentError unless @expected_calls[n][:args].size == x.size
@actual_calls[n] << { :retval => r, :args => x }
retval
}
self
end
#如何完成mock的呢,expect的三个参数存起来,然后,参数和预期的返回值,存起来,这里比较技巧
def verify
@expected_calls.each_key do |name|
expected = @expected_calls[name]
msg = "expected #{name}, #{expected.inspect}"
raise MockExpectationError, msg unless
@actual_calls.has_key? name and @actual_calls[name].include?(expected)
end
true
end
#验证