Mocking arguments passed through a yielding block

42 Views Asked by At

I am using Minitest to test some code that looks like this:

class MyClass
  self.my_method
    Faraday.post('https://example.com') do |req|
      req.headers['Content-Type'] = 'application/json'
      req.body = '{}'
    end
  end
end

I want to write a test which uses Minitest::Mock to check which parameters are passed.

If it was called using regular syntax (e.g. Faraday.post(args...) without the do |req| block) I would test it like this:

mock_post = Minitest::Mock.new
mock_post.expect(:call, {"sample" => "res"}, "https://example.com")
Faraday.stub :post, mock_post do
  MyClass.my_method
end

However, I'm not sure how to check the request params that are passed in using do |req|. For example, how would I check that req.headers['Content-Type'] = 'application/json?

0

There are 0 best solutions below