ruby-on-railsrubyunit-testingrspecstubs

How to stub method that doesn't belong to class in RSpec?


I'm working a project that uses a notify method that does does not belong to a method. I want to stub this method to help speed up my spec and keep my log clean. How can i do this?

lib/notify.rb

require 'json'
require 'rest-client'

def notify(*params)
  ...
end

Solution

  • Use a before block in your specs to stub the :notify method on subject:

    before do
      allow(subject).to receive(:notify)
    end