Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

533
Vistas
Rails Rspec - How to test if Service has been called in another Service

While writing tests, I stopped at trying to test Service in another Service. In such a situation, I should probably just check if Service has been called because it has already been tested elsewhere. I did a little research on the Internet and found something like have_received but I have no idea how to use it in my example.

check_service.rb

Class CheckService
 def initialize(params)
   @params = params
 end

 def self.call(params)
   new(params).call
 end

 def call
   CheckUser.call(params[:user_id])
 end
end

check_service_spec.rb

...

describe 'call' do
 let(:result) { CheckService.call(params) }
 let(:params) { { user_id: "100" } }
 let(:check_user) { instance_double(CheckUser) }

 before do
   allow(check_user).to receive(:call).and_return(true)
 end

 it do 
  result
  expect(check_user).to have_received(:call) 
 end
end
...

I was trying something like this (it's simple example), but I get error:

(InstanceDouble(CheckUser) (anonymous)).call(*(any args))
           expected: 1 time with any arguments
           received: 0 times with any arguments

Is there any option to test situation I presented?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Short anwser

describe 'call' do
 let(:result) { CheckService.call(params) }
 let(:params) { { user_id: "100" } }
 ## let(:check_user) { instance_double(CheckUser) } delete this

 before do
   allow(CheckUser).to receive(:call).and_return(true)
 end

 it do 
  result
  expect(CheckUser).to have_received(:call) 
 end
end

Alternative

I think a better way to test this is to use DI (Dependency Injection), so you pass CheckUser as a dependency to CheckService. I prefer to write the whole test inside the it block too!

class CheckService
  def initialize(params, check_handler:)
    @params = params
    @check_handler = check_handler
  end

  def self.call(params, check_handler: CheckUser)
    new(params, check_handler: check_handler).call
  end

  def call
    @check_handler.call(@params[:user_id])
  end
end
describe 'call' do
  it 'check user with params' do
    check_user = class_double(CheckUser)
    allow(check_user).to receive(:call).and_return(true)

    params = { user_id: "100" }
    CheckService.call(params, check_handler: check_user)

    expect(check_user).to have_received(:call)
  end
end

A blog post to read more about -> https://blog.testdouble.com/posts/2018-05-17-do-we-need-dependency-injection-in-ruby/

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda