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

214
Vistas
Stub a method inside other method
module A
  module B
    class C
       def fun(github) 
          return username
       end
       def gun(github)
          author=fun(github)
          #perfom some operation
          return count
       end
    end
end

I want to test gun function, but to get the value of author, it is making a call to function fun. Now, I do not want to call this function and instead stub it.

To achieve that, I wrote this line in the before do section while testing the function_1, But it's not working.

      allow(A::B::C).to receive(:fun).and_return("username")

I cannot figure out.

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

0

Both methods are instance methods, not class methods, therefore you need to stub the method on the instance.

The following might work for you:

let(:abc) { A:B.C.new }
let(:github) { ... }

before { allow(abc).to receive(:fun).and_return("username") }

it "..." do
  expect(abc.fun(github)).to eq(...)
  expect(abc).to have_received(:fun).with(github)
end
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