-
Notifications
You must be signed in to change notification settings - Fork 127
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
Tests For Geocoder #54
Comments
Heya @ihcsim ! Thanks so much for chiming in on how to increase the code coverage of One thing I really like about go and testing is when libraries provide well-defined interfaces that also serve well for testing purposes. For example, the type MockWriter struct {}
func (m MockWriter) Write(data []byte) (n int, err error) {
// Have your mocked functionality here
} In that regard, I think I'd like to see something along the lines of having the Geocoder Interface supplying the ability to mock out HTTP requests with more standard golang libraries. Perhaps something like: type Geocoder interface {
Geocode(string) Point
ReverseGeocode(lat, lng) string
HttpClient() *http.Client
} This way, we can set the http client in our tests and use the What do you think about that approach? |
@kellydunn I like your approach. I will give it a try, and get back to you. |
@kellydunn Following your thoughts on using the Expanding the Alternately (even this doesn't feel fully right), we can consider expanding the type Geocoder interface {
Geocode(string) Point
ReverseGeocode(lat, lng) string
Request(params string) ([]byte, error)
} The test coverage will be lower (since we mock out the
WDYT? |
Hi Kelly,
I notice that the test coverage for the
*Geocoder
is a bit low. Will you be interested in a PR where I refactor theGeocode()
andReverseGeocode()
methods to mock out the API calls.For example, in
GoogleGeocoder
, I update the struct to have a field function:Then I changed the
Geocode()
method to callg.sendRequest()
like this:This will makes it easier to test these methods. For example, I can mock the
sendRequest
field in the test like this:Obviously, this can be done for all the different types of
Geocoder
s. With this change, existing tests continue to pass, with test coverage for theGeocoder
s increased from around 30% to 78%.WDYT?
The text was updated successfully, but these errors were encountered: