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

408
Vistas
Rspec, how to test method with multiple service calls?

Class structure:

class Service1
  def foo
    puts 'foo'
  end
end

class Service2
  def bar
    3.times.each do 
      Service1.new().foo
    end
  end
end

I want to test that bar method of Service2 is called 3 times.

How to do it best in rspec3?

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

0

I would refactor the code to:

class Service1
  def self.foo
    new.foo
  end

  def foo
    puts 'foo'
  end
end

class Service2
  def bar
    3.times.each do 
      Service1.foo
    end
  end
end

And would then use a spec like this

describe "#bar" do
  let(:service1) { class_double("Service1") }

  it "calls Service1.foo three times" do
    expect(service1).to receive(:foo).exactly(3).times

    Service2.bar
  end
end
over 4 years ago · Santiago Trujillo Denunciar

0

You can achieve this by mocking new method

class Service1
  def foo
    puts 'foo'
  end
end

class Service2
  def bar
    3.times.each do 
      Service1.new().foo
    end
  end
end

Then the test:

let(:mocked_service) { instance_spy Service1 }

it "calls Service1.foo three times" do
  allow(Service1).to receive(:new).and_return mocked_service

  Service2.bar

  expect(mocked_service).to have_received(:foo).exactly(3).times
end

However, as mentioned in the comment - the necessity of using mocks is a first sign of flawed OO design, meaning that the problem you posted is merely a symptom. Refer to SOLID principles to find better design.

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