Question

Lets say I have a service class in my rails app. It doesn't really matter what it does but lets assume that it can be used for pushing notifications to clients.

# lib/services/event_pusher.rb
class EventPusher
  def initialize(client)
    @client = client
  end

  def publish(event)
    PusherGem.trigger(@client, event)
  end
end

I can now use this class in my controllers:

require "lib/services/event_pusher"

class WhateverController < ApplicationController
  def create
    @whatever = Whatever.new(params[:whatever])

    if @whatever.save
      EventPusher.new(current_user).publish('whatever:saved')
    end
  end
end

Now this service class makes a request to a third party when I call publish. I don't want that to happen when I'm running my tests.

The way I see it I have two options.

Option 1:
I have to remember to postfix all calls to EventPusher.trigger with an environment check. Remember that I could be calling this in every create/update/destroy action in my app.

if @whatever.save
  EventPusher.new(current_user).publish('whatever:saved') unless Rails.env.test?
end

Option 2:
I have to couple my service class to Rails.

def publish(event)
  PusherGem.trigger(@client, event) unless Rails.env.test?
end

Which is the correct option (or is there a secret option number 3)?

Was it helpful?

Solution

Are you using RSpec? If so, you can override the functionality of the EventPusher's publish method inside the test itself, like this:

EventPusher.any_instance.stub(:publish)

The above code replaces the original publish method with an empty method that returns nil. The method still exists and will still be called, but it just won't do anything within the scope of your tests.

If other code is expecting the publish method to return something, such as 'true' to indicate success, then you can add the following:

EventPusher.any_instance.stub(:publish).and_return(true)

Or, if you'd prefer to override the PusherGem's static trigger method, then use this slightly different syntax:

PusherGem.stub!(:trigger)

or

PusherGem.stub!(:trigger).and_return("something here, perhaps?")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top