简体   繁体   中英

RSpec - stubbing an instance method

I've added the following method into the middle of a project:

def finishes_after_venue_shuts?
  return unless venue && finish
  day = regular_day ? regular_day : start.strftime('%a').downcase
  finish > venue.openingtimes.where(default_day: day).pluck(:finish)[0]
end

This has caused 1000+ tests to fail within the project. They're failing with the following error code:

ArgumentError:
  comparison of ActiveSupport::TimeWithZone with nil failed

I've tried to stub out the method as follows but am apparently doing something wrong:

before do
  allow(Event.any_instance).to receive(:finishes_after_venue_shuts?).and_return(false)
end

What is the correct syntax for stubbing out the method and simply returning false rather than performing the code?

Thanks in advance.

You were close :)

allow_any_instance_of(Event)
  .to receive(:finishes_after_venue_shuts?)
  .and_return(false)

你也可以这样做:

Event.any_instance.stub(:finishes_after_venue_shuts).and_return(false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM