Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8952785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:57:04+00:00 2026-06-15T13:57:04+00:00

Lets say I have a service class in my rails app. It doesn’t really

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:57:06+00:00Added an answer on June 15, 2026 at 1:57 pm

    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?")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say that i have 2 pages: index.php and service.php index.php sends an http-post
Lets say I have a WCF service that a client can use to receive
Lets say I have this service: public class Service { ... public Service(IFactory factory)
I have an echo web service running on lets say http://localhost:8080/axis2/services/Service1 . This service
Let's say I have a RESTful, hypertext-driven service that models an ice cream store.
Lets say have this immutable record type: public class Record { public Record(int x,
I have a 2 WCF services that are exposing the same object. Lets say
Simple question : I have a service class (let's say helpersService ) and a
I have a very simple web service using Rack, without Rails that I will
Lets say I have a base class and child like this: class BaseType {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.