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

227
Vistas
Rspec expect to receive not working for object in array

I'm sure this is answered somewhere; I can't seem to phrase my google search right though. I'm trying to test that a method is called on an object, but the method isn't called on the specific object in the spec. The method is called on the last item in a collection, which I've confirmed is the same underlying object as the one in the spec. I'm not sure how clear that was, so here is an example:

expect(@email).to receive(:send) # fails
puts @user.emails.last == @email # true
@user.emails.last.send

As a sanity check, this spec passes. However the code I'm testing has @user.emails.last.send in it, so I'm trying to figure out how to make the spec above pass.

expect(@email).to receive(:send) # passes
@email.send

Edit:

@user.emails.last.equal?(@email) returns false, so as suspected by @spickermann and @Grzegorz the @user.emails.last and @email are two instances of the same object. So I guess what I'm asking is how can I test that the send method was called on a specific object (ignoring what particular instance of that object it was called on). My question is actually the same as this one that I just found Rspec: Test if instance method was called on specific record with ActiveRecord.

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

0

It's possible that == method is defined on the mail object in a way that it returns true if some attributes are the same, but it doesn't care if the object is the same.

@user.emails.last == @email

This is the case with a simple string:

>> "d" == "d"
=> true
>> "d".object_id == "d".object_id
=> false

So It is possible that @mail and @user.emails.last are different objects in memory, but return true when using == method.

You can confirm that there's nothing wrong with your expectation like this:

expect(@user.emails.last).to receive(:send) # should pass now
@user.emails.last.send

You didn't share much code for context, so it's not clear what a "good" solution in your case could be. But I hope this will point you in the right direction.

over 4 years ago · Santiago Trujillo Denunciar

0

In my case I'm able to work around this by returning the Email instance (@user.emails.last) from the send_email method and ensuring that is the same object as the @email object in the spec. E.g.,:

# The `send_email` method calls `@user.emails.last.send` and returns `@user.emails.last`
email = @user.send_email
expect(email).to eq(@email) # passes!
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