待测试类
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
时间: 2024-10-24 22:57:17