Skip to content

fix: add test for all sms providers#676

Merged
kangmingtay merged 5 commits intomasterfrom
km/test-sms-provider
Sep 16, 2022
Merged

fix: add test for all sms providers#676
kangmingtay merged 5 commits intomasterfrom
km/test-sms-provider

Conversation

@kangmingtay
Copy link
Copy Markdown
Member

@kangmingtay kangmingtay commented Sep 12, 2022

What kind of change does this PR introduce?

  • Introduce a custom HttpClient interface which can be overridden in tests.
  • For tests in sms_provider, we can overwrite the native http.Client with a MockHttpClient and define the expected API behaviour from the 3rd-party sms providers with handleApiRequest.
  • Used gock for mocking external API requests instead

To-Do

  • Implement the tests for SendSms for messagebird, textlocal and vonage

@kangmingtay kangmingtay self-assigned this Sep 12, 2022
@kangmingtay kangmingtay requested a review from a team as a code owner September 12, 2022 06:58
@kangmingtay kangmingtay changed the title fix: add test for twilio sms_provider [WIP] fix: add test for twilio sms_provider Sep 12, 2022
Comment thread api/sms_provider/sms_provider_test.go Outdated
@kangmingtay kangmingtay changed the title [WIP] fix: add test for twilio sms_provider fix: add test for twilio sms_provider Sep 16, 2022
@kangmingtay kangmingtay changed the title fix: add test for twilio sms_provider fix: add test for all sms_provider Sep 16, 2022
@kangmingtay kangmingtay changed the title fix: add test for all sms_provider fix: add test for all sms providers Sep 16, 2022
@kangmingtay kangmingtay merged commit de6cd79 into master Sep 16, 2022
@kangmingtay kangmingtay deleted the km/test-sms-provider branch September 16, 2022 07:30
@github-actions
Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 2.16.6 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

}

func (ts *SmsProviderTestSuite) TestTwilioSendSms() {
defer gock.Off()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually use defer gock.OffAll() so that unmatched requests don't carry over to the next test. https://pkg.go.dev/github.com/h2non/gock#OffAll

err = twilioProvider.SendSms(phone, message)
require.Equal(ts.T(), c.ExpectedError, err)
})
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another good check at the end of a test is assert.Empty(t, gock.GetUnmatchedRequests()). It ensures that all expectations you've set with gock are called once.

I also find its default logging a little unclear, so I created a helper to print out unmatched requests.

uxodb pushed a commit to uxodb/auth that referenced this pull request Nov 13, 2024
* refactor: use custom HttpClient interface

* test: add test for twilio SendSms

* use gock for mocking requests

* test twilio error edge cases
LashaJini pushed a commit to LashaJini/auth that referenced this pull request Nov 13, 2024
* refactor: use custom HttpClient interface

* test: add test for twilio SendSms

* use gock for mocking requests

* test twilio error edge cases
LashaJini pushed a commit to LashaJini/auth that referenced this pull request Nov 15, 2024
* refactor: use custom HttpClient interface

* test: add test for twilio SendSms

* use gock for mocking requests

* test twilio error edge cases
cemalkilic pushed a commit that referenced this pull request Aug 7, 2025
* refactor: use custom HttpClient interface

* test: add test for twilio SendSms

* use gock for mocking requests

* test twilio error edge cases
xeladotbe pushed a commit to xeladotbe/supabase-auth that referenced this pull request Sep 27, 2025
* refactor: use custom HttpClient interface

* test: add test for twilio SendSms

* use gock for mocking requests

* test twilio error edge cases
fadymak pushed a commit that referenced this pull request Sep 30, 2025
* refactor: use custom HttpClient interface

* test: add test for twilio SendSms

* use gock for mocking requests

* test twilio error edge cases
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