码迷,mamicode.com
首页 > 其他好文 > 详细

rspec 针对元编程的测试

时间:2017-08-18 19:50:36      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:div   针对   color   new   lin   ddr   name   ruby   元编程   

待测试类
 1 class MyClassDynamic
 2   DEFAULT_METHOD_NAME = :my_dynamic_method
 3 
 4   def initialize(method_name = nil)
 5     method_name = DEFAULT_METHOD_NAME unless method_name
 6 
 7     method_definition = Proc.new { method_name.to_s }
 8     self.class.send(:define_method, method_name, method_definition)
 9   end
10 end

rspec

 1 require spec_helper
 2 
 3 shared_examples "a dynamic my class" do |method_name = nil|
 4   let(:address) { method_name.nil? ? MyClassDynamic.new : MyClassDynamic.new(method_name) }
 5 
 6   it "creates the method" do
 7     address.should respond_to method_name
 8   end
 9 
10   it "returns the method name as a string" do
11     address.send(method_name).should eq(method_name.to_s)
12   end
13 end
14 
15 describe MyClassDynamic do
16   it_behaves_like "a dynamic my class", MyClassDynamic::DEFAULT_METHOD_NAME
17   it_behaves_like "a dynamic my class", :my_dynamic_method
18   it_behaves_like "a dynamic my class", :your_dynamic_method
19 end

 

rspec 针对元编程的测试

标签:div   针对   color   new   lin   ddr   name   ruby   元编程   

原文地址:http://www.cnblogs.com/lizhenjiang/p/7391159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!