在 Rspec 中测试嵌套的 Class 方法
Test a Nested Class Method in Rspec
有没有办法测试是否存在从另一个 class 方法调用的 class 方法?
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
我认为您已经从测试示例中删除了行 expect(AnimalRecord).to receive(:event)
。 AnimalRecord
上似乎没有定义这样的方法,但您正在尝试 "expect" 它。
有没有办法测试是否存在从另一个 class 方法调用的 class 方法?
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
我认为您已经从测试示例中删除了行 expect(AnimalRecord).to receive(:event)
。 AnimalRecord
上似乎没有定义这样的方法,但您正在尝试 "expect" 它。