Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking parameters of method calls to a test double in RSpec

Tags:

ruby

rspec

Is it possible to check if the parameters passed to the parameters of a method call fulfill certain constraints. I would like to do something like

my_double = double("MyObject")
my_double.should_receive(:mocked_method).with{ <something that has an attribute called name and value "john"> }

Thanks a lot in advance.

Edit: I'll try to clarify a bit what I want to accomplish

What I want is to check that a given method of a mock was called passing an object that fulfils some conditions

like image 539
Rafa de Castro Avatar asked Sep 03 '25 05:09

Rafa de Castro


1 Answers

For the record. What I wanted to accomplish can be made with

  @test_double.should_receive(:send_mail) do |mail|
    mail.to.should eq(["[email protected]"])
    mail.body.decoded.should include "Error"
  end

And the code should call the method send_mail of the given object with a parameter that fulfills the conditions specified in the block.

like image 163
Rafa de Castro Avatar answered Sep 05 '25 00:09

Rafa de Castro