【问题标题】:While running the rspec it shows does not implement( allow_any_instance_of )运行 rspec 时,它显示未实现(allow_any_instance_of)
【发布时间】:2022-01-02 13:55:11
【问题描述】:

我的 rspec 行

allow_any_instance_of(School).to receive(:admission).and_return(:body)

我的录取方法在我的lib/school.rb 模块学校

def self.admission()

像这样,但是当我运行 rspec 时它会抛出类似的错误

Failure/Error: allow_any_instance_of(School).to receive(:admission),and_return(:body)
       School does not implement #admission

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 rspec rubygems


    【解决方案1】:

    admission 在这个例子中是一个类方法(注意它前面的self.)。这意味着允许任何 instance 接收该方法是没有意义的。

    你有两个选择:

    1. 将其保留为类方法,但将期望更改为 allow(School).to receive(:admission)...
    2. 通过删除self. 来制作admission 和实例方法

    这取决于您的应用程序哪种方法更有意义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-22
      • 2011-06-18
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多