zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    # ES6基础
    # yarn简单使用
    # laravel框架中的配置
    需求概述开发进度09
    需求概述开发进度08
    需求概述开发进度07
    需求概述开发进度06
    需求概述开发进度05
    需求概述开发进度04
    需求概述开发进度03
  • 原文地址:https://www.cnblogs.com/lizhenjiang/p/7391159.html
Copyright © 2011-2022 走看看