Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

minitest - pass any argument to expect

Tags:

ruby

minitest

I have the following code that I am using to mock a call to a class method:

def test_calls_update_profile_job_for_a_lead
  input = ContactInput.new valid_attributes

  mock = MiniTest::Mock.new

  use_case = CreateContact.new user, input, mock

  mock.expect(:perform_async, nil, [user.id, 1, ::Contact])

  use_case.run!

  assert mock.verify
end

The problem is that I am having to pass in the specific values -

[user.id, 1, ::Contact]

to make the test pass.

Is there a way that I don't have to specify the exact values or maybe at least constrain what the arguments are. I don't want to check the exact arguments, I just want to make sure that the method was called.

like image 493
dagda1 Avatar asked Jul 15 '14 16:07

dagda1


1 Answers

According to the docs:

args is compared to the expected args using case equality (ie, the '===' operator), allowing for less specific expectations.

For instance,

mock = MiniTest::Mock.new
mock.expect(:perform_async, 'goodbye', [Integer, Integer, String])
puts mock.perform_async(1, 1, 'hello')  #=>goodbye 
puts mock.perform_async(1, 1, 1)  #=>MockExpectationError
like image 182
7stud Avatar answered Nov 03 '22 11:11

7stud