ruby rspec_context_runner.rb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby rspec_context_runner.rb相关的知识,希望对你有一定的参考价值。

class ContextRunner
  DANGER = EntityModel.create_access_model('danger')
  DANGER_ENTITY = Entity.find_by(table_name: 'danger')
  include FactoryGirl::Syntax::Methods
  def initialize(opts={})
    opts.each do |var, value|
      let(var) do
        value
      end
    end
  end
  def let!(name)
    result = yield
    self.class.send(:define_method, name) do
      result
    end
  end

  def let(name)
    self.class.send(:define_method, name) do
      if instance_variable_defined?(let_instance_var(name))
        instance_variable_get(let_instance_var(name))
      else
        result = yield
        instance_variable_set(let_instance_var(name), result)
        result
      end
    end
  end

  def let_instance_var(name)
    "@___let_var_#{name}"
  end

  def run(name)
    ActiveRecord::Base.transaction do
      block = RspecContextWrapper.get(name)
      instance_exec(&block)
      self
    end
  end
  alias include_context run

  def self.init
    new(stage_1: Stage.first, danger_dimension: DANGER_ENTITY, danger_low: DANGER.first, danger_high: DANGER.last)
  end
end


module RspecContextWrapper
  CONTEXTS = {}
  module_function
  def add(name, &block)
    CONTEXTS[name] = block
    RSpec.shared_context(name, &block)
  end

  def get(name)
    CONTEXTS[name]
  end
end

以上是关于ruby rspec_context_runner.rb的主要内容,如果未能解决你的问题,请参考以下文章

Ruby运算符

Ruby 25 岁了!Ruby 之父说 Ruby 3 有望 3 倍提速

如何学习ruby?Ruby学习技巧分享

ruby Ruby脚本,看看是否用openssl编译了ruby

什么是ruby?

ruby和ruby ee