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.
CodePudding user response:
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.
CodePudding user response:
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!