我正在重构一个臃肿的 Controller ,该 Controller 为旋转木马提供多态模型。我正在尝试构建一个类方法来处理查找和返回可轮播的项目。
在我的 RSPEC 测试中,我想 stub 方法“is_something?”在根据参数找到的 field 上。
def self.find_carouselable(params)
.......
elsif params[:venue_id].present?
venue=Venue.friendly.find(params[:venue_id])
if venue.is_something?
do this
else
do that
end
end
end
我无法弄清楚如何 stub 作为输入数据的结果创建的对象 - 我不确定这是否称为 stub 或模拟?
context "carouselable is a venue" do
before do
allow(the_venue).to receive(:is_something?).and_return(true)
end
it "returns the instance of the carouselable object" do
expect(CopperBoxCarouselItem.find_carouselable(venue_params)).to eq the_venue
end
end
非常感谢
最佳答案
你应该能够做到:
allow_any_instance_of(Venue).to receive(:is_something?).and_return(true)
关于ruby-on-rails - Rspec 如何 stub 在方法中创建的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27972623/