免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2874 | 回复: 1

rspec测试 [复制链接]

论坛徽章:
0
发表于 2012-03-05 20:13 |显示全部楼层
本帖最后由 feiyang10086 于 2012-03-06 11:47 编辑

ruby block的用途和理解







lambdarubyrspec
在论坛讨论问题,自己写体会抄回来

常用到的场合:
1. scope 当需要动态参数时
Ruby代码
  1. scope :aged_0, lambda{ where("created_at IS NULL OR created_at < ?", Date.today + 30.days).joins(:owner) }   
  2. scope :aged_1, lambda{ |d_time|where("created_at IS NULL OR created_at < ?", d_time).joins(:owner) }  

  3. scope :aged_0, lambda{ where("created_at IS NULL OR created_at < ?", Date.today + 30.days).joins(:owner) }
  4. scope :aged_1, lambda{ |d_time|where("created_at IS NULL OR created_at < ?", d_time).joins(:owner) }
复制代码
2. rspec测试
Ruby代码
  1. lambda{post :create, :user => @attr}.should_not change(User, :count)  

  2. lambda{post :create, :user => @attr}.should_not change(User, :count)
复制代码
参考文献,很多推荐
http://www.robertsosinski.com/20 ... -procs-and-lambdas/
block本质是匿名方法,和hash loop class recursion一样就是一个编程的工具,没有的话程序都可以写,就是特别的场合,有特别适用的地方。
那么,block匿名方法什么时候适用呢?方便理解也举个例子吧,
有两组数据,可能要进行不同处理

Ruby代码
  1. def cross_loop(array_a, array_b, process)   
  2.   array_a.each do |element_a|   
  3.     array_b.each do |element_b|   
  4.       process.call(element_a, element_b)   
  5.     end  
  6.   end  
  7. end  
  8.   
  9. cross_loop([1,2,3], [3,4,5], lambda{|x,y| puts x + y})   
  10. cross_loop([1,2,3], [3,4,5], lambda{|x,y| puts x * y})  

  11. def cross_loop(array_a, array_b, process)
  12.   array_a.each do |element_a|
  13.     array_b.each do |element_b|
  14.       process.call(element_a, element_b)
  15.     end
  16.   end
  17. end

  18. cross_loop([1,2,3], [3,4,5], lambda{|x,y| puts x + y})
  19. cross_loop([1,2,3], [3,4,5], lambda{|x,y| puts x * y})
复制代码
说不用lambda行不行,当然,最基本可以在cross_loop里面调用一个函数。只不过重用的时候不好,因为没有process的参数。
当然,也可以不用lambda又动态传入process,写个process.call然后,再写些process函数也可以。

Ruby代码
  1. def cross_loop(array_a, array_b, process)   
  2.   array_a.each do |element_a|   
  3.     array_b.each do |element_b|   
  4.       self.send(process, element_a, element_b)   
  5.     end  
  6.   end  
  7. end  
  8. def plus(a, b); puts a + b; end  
  9. def multiply(a, b); puts a * b; end  
  10. cross_loop([1,2,3], [3,4,5], 'plus')   
  11. cross_loop([1,2,3], [3,4,5], 'multiply')  

  12. def cross_loop(array_a, array_b, process)
  13.   array_a.each do |element_a|
  14.     array_b.each do |element_b|
  15.       self.send(process, element_a, element_b)
  16.     end
  17.   end
  18. end
  19. def plus(a, b); puts a + b; end
  20. def multiply(a, b); puts a * b; end
  21. cross_loop([1,2,3], [3,4,5], 'plus')
  22. cross_loop([1,2,3], [3,4,5], 'multiply'
复制代码
)

论坛徽章:
0
发表于 2012-03-05 20:44 |显示全部楼层
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP