使用spring prelaoder

时间:2017-12-24 02:58:36

标签: ruby-on-rails ruby rspec

我在Factory女孩定义中定义了一个类,如下所示

factory :user, class: User do
 trait :successful do
  after(:build) do |u|
    u.calling_method_from_external_concerns_or_mockey_pached_method
  end
end

并按如下方式创建了因子对象

@user        = build(:user,         :successful)

它总是在没有弹簧预加载器的情况下通过,但是当我用弹簧运行时,它只是第一次通过,在第一次之后我在回购中做了任何更改它在创建工厂时引发了一个未定义的方法(calling_method_from_external_concerns_or_mockey_pached_method)女孩的对象。注意:当我在我的仓库中进行任何更改(添加额外的行或任何其他更改)时会发生这种情况,如果我再次停止预加载器运行它会通过。

在后构建块

 after(:build) do |u|
    u.methods.include?(:calling_method_from_external_concerns_or_mockey_pached_method)
    # returns false

    User.new.include?(:calling_method_from_external_concerns_or_mockey_pached_method)
    # returns true
 end

未定义的方法是在测试之前包含的模块中定义的

  module Mock
   def calling_method_from_external_concerns_or_mockey_pached_method; end
  end

在另一个类文件中也尝试了如下的猴子补丁,我得到了与之前提到的相同的模式

  class User
     def calling_method_from_external_concerns_or_mockey_pached_method; end
  end

0 个答案:

没有答案