MiniTest::Mock 二,进一步示例分析
发布日期:2021-09-29 20:09:24 浏览次数:8 分类:技术文章

本文共 2023 字,大约阅读时间需要 6 分钟。

据说,如果对mock stub 和fakes概念不熟悉的话可以看看[url=http://en.wikipedia.org/wiki/Mock_object]这个[/url] 我自己看了一下,没懂。还是看例子容易些
例如,有这么个东西需要测试
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的[url=https://github.com/seattlerb/minitest/blob/master/lib/minitest/mock.rb]源代码[/url]
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 #验证

转载地址:https://blog.csdn.net/horace_lee/article/details/84086070 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:ruby 1.9 简单的文件操作
下一篇:MiniTest::Mock Ruby1.9 标准库支持

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月16日 23时20分23秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

spring boot 与 Ant Design of Vue 实现删除按钮(十八) 2019-04-27
spring boot 与 Ant Design of Vue 实现角色管理布局以及角色的列表(十九) 2019-04-27
spring boot 与 Ant Design of Vue 实现新增角色(二十) 2019-04-27
spring boot 与 Ant Design of Vue 实现修改角色(二十一) 2019-04-27
spring boot 与 Ant Design of Vue 实现删除角色(补二十一) 2019-04-27
spring boot 与 Ant Design of Vue 实现组织管理布局的实现(二十二) 2019-04-27
spring boot 与 Ant Design of Vue 实现左侧组织树(二十三) 2019-04-27
spring boot 与 Ant Design of Vue 实现新增组织(二十四) 2019-04-27
spring boot 与 Ant Design of Vue 实现修改组织(二十五) 2019-04-27
spring boot 与 Ant Design of Vue 实现删除组织(二十六) 2019-04-27
spring boot 与 Ant Design of Vue 实现获取用户列表(二十七) 2019-04-27
spring boot 与 Ant Design of Vue 实现删除用户(三十) 2019-04-27
spring boot 与 Ant Design of Vue 鉴权体系获取用户信息的实现(三十二) 2019-04-27
Druid连接池实现自定义场景的多数据库的连接 2019-04-27
PL/SQL数据库管理工具的使用 2019-04-27
带你玩转属于自己的spring-boot-starter系列(一) 2019-04-27
带你玩转属于自己自己的spring-boot-starter系列(二) 2019-04-27
带你玩转属于自己的spring-boot-starter系列(三) 2019-04-27
基于SnowFlake算法如何让分库分表中不同的ID落在同一个库的算法的实现 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之分表解决方案(一) 2019-04-27