rspec 针对元编程的测试
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rspec 针对元编程的测试相关的知识,希望对你有一定的参考价值。
待测试类
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 针对元编程的测试的主要内容,如果未能解决你的问题,请参考以下文章
Rspec: everyday-rspec实操。第7章使用请求测试-测试API