New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add some means to assert that a stub was called #434
Comments
/cc @jkakar I know you have brought this up a couple times. Could you summarize what you have in mind and/or hook me up with a couple links to where you rolled your own? I'd like to do something here, but want to have a little better sense of the design goal before I get too far in to it. Thanks! |
Fundamentally, when I ask Excon to stub a request I expect it to blow I include this test helper in tests that mock Excon: module ExconHelper
def setup
super
Excon.stubs.clear
Excon.defaults[:mock] = true
end
def teardown
# FIXME This is a bit ugly, but Excon doesn't provide a builtin way to
# ensure that a request was invoked, so we have to do it ourselves.
# Without this, and the Excon.stubs.pop calls in the tests that use this
# helper, tests will pass if request logic is completely removed from
# application code. -jkakar
assert(Excon.stubs.empty?, 'Expected HTTP requests were not made.')
super
end
end I explicitly pop the stubbed request out of the class LinkTest < MiniTest::Unit::TestCase
include ExconHelper
# Link.run invokes a request against the service identified by the URL. The
# path is left unchanged when parameters aren't required and the username
# and password from the URL are passed using HTTP basic auth.
def test_run_without_parameters_and_with_empty_response
Excon.stub(method: :get) do |request|
assert_equal('Basic dXNlcm5hbWU6c2VjcmV0',
request[:headers]['Authorization'])
assert_equal('example.com', request[:host])
assert_equal(443, request[:port])
assert_equal('/resource', request[:path])
Excon.stubs.pop
{status: 200, body: ''}
end
schema = Heroics::Schema.new(SAMPLE_SCHEMA)
link = Heroics::Link.new('https://username:secret@example.com',
schema.resource('resource').link('list'))
assert_equal(nil, link.run)
end
end It's a bit ugly but it ensures that the tests are reliable. |
@jkakar awesome, thanks for the context. Moving past the current, unfortunate state, thoughts on what invocation might look like in an idealized future? |
I like the API Excon provides for stubbing calls. It's simple and |
Good call. I'll probably start with something like #verify_stubs (when I get a chance) and work from there. Do you think boolean is good-enough? ie I've seen some tests where it would be something like |
This issue has been automatically marked stale due to inactivity. It will be closed if no further activity occurs. Thank you for your contributions. |
No description provided.
The text was updated successfully, but these errors were encountered: