在Rspec中测试嵌套类方法

问题描述 投票:0回答:1

是否有方法可以测试是否存在从另一个类方法调用的类方法?

class AnimalRecord < ApplicationRecord
  COLLAR_ID = AnimalCollar::ID
  belongs_to :animal
  serialize :json, JSON
  scope :collar_id, -> { for_collar(COLLAR_ID) }

  def self.current_record(animal)
    animal_info = AnimalRecord.collar_id.first
    calculate_nutrients(animal_info)
  end

  def self.calculate_nutrients(animal)
    code result
  end
end

我可以从current_record中测试current_record方法。但是测试calculate_nutrients方法的正确方法是什么?

我有这个:

context "test record" do
  before do
    @animal = create(:animal... )
    @animal_record = create(:animal_record...)
  end

  it "calls #calculate_nutrients" do 
    expected_response = responseHere
    expect(AnimalRecord).to receive(:calculate_nutrients).and_return(expected_response)

    AnimalRecord.current_record(@animal)
  end

但是我收到这样的错误消息:

    expected: 1 time with any arguments
       received: 0 times with any arguments
ruby-on-rails rspec
1个回答
1
投票

我认为您确实从测试示例中删除了行expect(AnimalRecord).to receive(:event)。似乎AnimalRecord上没有定义这样的方法,但是您正在尝试“期望”它。

© www.soinside.com 2019 - 2024. All rights reserved.