mike927
mike927

Reputation: 774

RSpec - Check whether method was called

My goal is to write rspec test that check whether method was called.

notify(result) if notification_allowed?(result)

Both notification_allowed? and notify are private methods. More precisely I need a test that check whether notify method has was called. I've tried to do something like below but it doesn't seem to be right.

subject { described_class.new }

it do
  expect(subject).to receive(:notify)
  subject.send(:notification_allowed?, true)
end

Upvotes: 1

Views: 564

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36860

Calling notification_allowed? doesn't do anything except return the result of notification_allowed? It's not related to notify

You would need to call the function that contains the expression you want to test.

For example, the method might be...

def check_and_notify(result)
  notify(result) if notification_allowed?(result)
end

so the test would be...

subject { described_class.new }

it 'calls notify' do
  expect(subject).to receive(:notify)
  subject.send(:check_and_notify, true)
end

Upvotes: 2

Related Questions