Skip to content
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

Change some error tests to use assert_raises #847

Merged
merged 1 commit into from
Sep 4, 2019

Conversation

brandur
Copy link
Contributor

@brandur brandur commented Sep 4, 2019

Previously, we had quite a few error tests that were written like this:

begin
  client.execute_request(:post, "/v1/charges")
rescue Stripe::InvalidRequestError => e
  assert_equal(404, e.http_status)
  assert_equal(true, e.json_body.is_a?(Hash))
end

The trouble with that pattern is that although it'll usually work,
the test will incorrectly pass if no error at all is thrown because the
rescue never activates and therefore the assertions never run.

We change them to use assert_raises like so:

e = assert_raises Stripe::InvalidRequestError do
  client.execute_request(:post, "/v1/charges")
end
assert_equal(404, e.http_status)
assert_equal(true, e.json_body.is_a?(Hash))

The weird part is that many of the tests were already using
assert_raises, so here we're just converting them all over to use the
same convention.

I've also made a few whitespace tweaks. None of them are significant,
but they were an attempt to standardize a little on the whitespace
layout of many of these tests which were similar.

r? @ob-stripe

Previously, we had quite a few error tests that were written like this:

``` ruby
begin
  client.execute_request(:post, "/v1/charges")
rescue Stripe::InvalidRequestError => e
  assert_equal(404, e.http_status)
  assert_equal(true, e.json_body.is_a?(Hash))
end
```

The trouble with that pattern is that although they'll _usually_ work,
the test will incorrectly pass if no error at all is thrown because the
`rescue` never activates and therefore the assertions never run.

We change them to use `assert_raises` like so:

``` ruby
e = assert_raises Stripe::InvalidRequestError do
  client.execute_request(:post, "/v1/charges")
end
assert_equal(404, e.http_status)
assert_equal(true, e.json_body.is_a?(Hash))
```

The weird part is that many of the tests were already using
`assert_raises`, so here we're just converting them all over to use the
same convention.

I've also made a few whitespace tweaks. None of them are significant,
but they were an attempt to standardize a little on the whitespace
layout of many of these tests which were similar.
@brandur-stripe
Copy link
Contributor

TY!

@brandur-stripe brandur-stripe merged commit 1f80da4 into master Sep 4, 2019
@brandur-stripe brandur-stripe deleted the brandur-assert-raises branch September 4, 2019 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants