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)?