use*_*832 2 ruby rspec ruby-on-rails
我正在重构一个臃肿的控制器,为旋转木马提供多态模型.我正在尝试构建一个类方法来处理查找和返回可转载的项目.
在我的RSPEC测试中,我想要保留方法'is_something?' 在由于参数而被发现的场地上.
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
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何存根由于输入数据而创建的对象 - 我不确定这是否称为存根或嘲弄?
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
Run Code Online (Sandbox Code Playgroud)
非常感谢
你应该能够做到:
allow_any_instance_of(Venue).to receive(:is_something?).and_return(true)
Run Code Online (Sandbox Code Playgroud)
你只需要存根 Venue 位,就像这样
before do
allow(Venue).to receive(:friendly).and_return(some_venues)
allow(some_venues).to receive(:find).and_return(venue)
allow(venue).to receive(:is_something?).and_return(true)
end
Run Code Online (Sandbox Code Playgroud)