Created
July 11, 2017 02:53
-
-
Save IanMitchell/33d8da9da88194d5c4f3a64e36e3aed3 to your computer and use it in GitHub Desktop.
Minitest Stumbling Block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
def b(params) | |
payload = config.merge(params) | |
uri = get_endpoint | |
Net::HTTP.post(uri, payload) | |
end | |
end | |
# Wanted to test A::b by stubbing out Net::HTTP and examining what it was being called with. | |
# Maybe not the best way of approaching the test, but seemed logical to me. Ended up with the | |
# following, but struggled finding out how to stub out Net::HTTP and examine the parameters the stub | |
# was called with. Googling the problem resulted in rspec solutions, but not a lot of Minitest | |
# tutorials beyond "check out Mocha!" which also didn't have a clearcut answer on how to do this. | |
@mock = Minitest::Mock.new | |
@mock.expect(:post, true, [uri, expected_payload]) | |
Net::HTTP.stub :post, ->(uri, payload) { @mock.post(uri, payload) } do | |
A::b | |
end | |
@mock.verify | |
# Works now though! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
But I still don't get much value out of these types of tests.