测试方法-Ruby / RSpec与Java / Mockito
||
我正在尝试编写一些代码,如下例所示,但是用Java而不是Ruby和Mockito而不是RSpec。
require \'rubygems\'
require \'rspec\'
class MyUtils
def self.newest_file(files)
newest = nil
files.each do |file|
if newest.nil? || (File.new(file).mtime > File.new(newest).mtime)
newest = file
end
end
newest
end
end
describe MyUtils do
it \"should return the filename of the file with the newest timestamp\" do
file_a = mock(\'file\', :mtime => 1000)
file_b = mock(\'file\', :mtime => 2000)
File.stub(:new).with(\"a.txt\").and_return(file_a)
File.stub(:new).with(\"b.txt\").and_return(file_b)
MyUtils.newest_file([\'a.txt\', \'b.txt\']).should == \'b.txt\'
end
end
在RSpec中,我可以对File.new存根,但是我认为我不能在Mockito中做到这一点?
我是否应该使用工厂来创建File对象,将工厂作为依赖项注入,然后在工厂中存根进行测试?
没有找到相关结果
已邀请:
2 个回复
漂截嘘
豹芜澈