I'm trying to write a test where I need the value created by the expected block to write the assertion.
class Identification < ApplicationRecord
include Wisper::Publisher
after_save :publish_identification_declined
private
def publish_identification_declined
if status_previously_changed? && status == "declined"
broadcast(:identification_declined, identification: self)
end
end
end
I tried to do something like this but unfortunately identification_a
ends up not being set.
require "rails_helper"
RSpec.describe Identification do
it "publish event identification_declined" do
identification_a = nil
expect { identification_a = create(:identification, :declined, id: 1) }
.to broadcast(:identification_declined, identification: identification_a)
end
end
I also have a feeling that this might not be a good idea.
An alternative could be using the instance_of
matcher but then I don't know how to check if it's the right instance.