Reputation: 6674
I have a model with a simple :after_update
callback that is not firing for some reason. I have a test that checks whether the function is called and it passes, but for some reason nothing actually happens.
class HealthProfile < ApplicationRecord
after_update :send_office_email
def send_office_email
p "sending office email"
email = MailjetService.new(ID)
Rails.application.routes.default_url_options[:host] = 'host_name'
message = email.send_email('email', self.location.email, "New Health Profile: #{self.name}", {
"office_name" => self.location.name,
"name" => self.name,
"url" => Rails.application.routes.url_helpers.health_profile_url(self)
})
p message
end
end
Test:
require 'rails_helper'
RSpec.describe HealthProfile, type: :model do
before(:each) do
@hp = build(:health_profile)
end
it 'emails office on update (complete - w/ signature)' do
p 1
@hp.save
p 2
expect(@hp).to receive(:send_office_email)
p "updating signature to true..."
@hp.update(signature: 't')
p 3
end
end
In my test output I see the following:
1
2
"updating signature to true..."
3
But I never see sending office email
. The test passes, showing that the model received the callback, but nothing in the callback ran. What am I missing?
Upvotes: 0
Views: 50
Reputation: 4010
Actually, I believe your test is working. If it wasn't your test would be failing with expected to receive "send_office_email" but nothing was called
.
expect to_receive
is stubbing the method on the object which results in a dummy method that doesn't call your original method.
Try this.
require 'rails_helper'
RSpec.describe HealthProfile, type: :model do
let(:hp) { build(:health_profile) }
it 'emails office on update (complete - w/ signature)' do
p 1
hp.save
p 2
p "updating signature to true..."
hp.update(signature: 't')
p 3
end
end
Upvotes: 1