DEV Community

Augusts Bautra
Augusts Bautra

Posted on

Disable specific callback in spec

Sometimes a service is called in some after_commit callback and it triggers already during setup in that service spec as we're trying to get to arguments for the service (usually some record ids).

If only there was a way to disable the callback...

There is! Override the callback method for the one spec instance:

before
  # some safety if callback name gets changed
  abort(":some_callback suddenly missing!") unless model_instance.respond_to?(:some_callback)

  def model_instance.some_callback
    nil
  end
end
Enter fullscreen mode Exit fullscreen mode

More on singleton redefinition in SO thread.

Top comments (1)

Collapse
 
sawyerwolfe profile image
Sawyer Wolfe

This is a neat approach! Have you tried using any other methods to disable callbacks, and how did they compare to this one?