I have code that looks like this in a method that I am trying to test:
service = if user_activator?
UserActivationService.new(subscription.user, cookies: cookies)
elsif sub_activator?
SubscriptionActivationService.build(subscription: subscription, cookies: cookies)
end
I am trying to stub service to be an instance of UserActivationService and then try to see if it receives a method with some code that looks like this:
expect(user_activation_service_instance).to receive(:activate).with(hash_including(
app_context: app_context,
cookies: cookies,
subscription: subscription
))
But is there a way to stub a local variable to be an instance of UserActivationService??
Here are some ways that I think may work?
First, should I extract that conditional local variable set into a method like this (this feels fishy):
def service
if user_activator?
UserActivationService.new(subscription.user, cookies: cookies)
elsif sub_activator?
SubscriptionActivationService.build(subscription: subscription, cookies: cookies)
end
end
then I can do:
let(:user_activation_service_instance) { UserActivationService.new(subscription.user, cookies: cookies) }
allow(<something that returns an instance of the class I am testing>).to receive(:service) { user_activation_service_instance }
then
expect(user_activation_service_instance).to receive(:activate).with(hash_including(
app_context: app_context,
cookies: cookies,
subscription: subscription
))
Is this kosher?
Aucun commentaire:
Enregistrer un commentaire