Mocking strategy for external API libraries: google/go-github example

Written by
Link to Post

https://www.reddit.com/r/golang/comments/aeyal4/mocking_strategy_for_external_api_libraries/

Hello everyone. Fairly new to Go here.

I read lot of content about mocking strategies, but I still don’t get how to mock external-provided libraries. For example, I am writing an application that calls GitHub API v3 using the well-known https://github.com/google/go-github. I want to mock calls to the library in order to test my business logic. Here’s my function:

import ( // ... "github.com/google/go-github/v21/github" ) func DoSomething(...) (result Result, err error) { ... fileContents, _, _, err := github.Repositories.GetContents(ctx, owner, repo, filename, nil) if err != nil { log.Fatal(err) } // Do something with fileContents return result, err } 

I want to test the return value of DoSomething by mocking github.Repositories.GetContents.

submitted by /u/-fly-
[link] [comments]

Article Tags:
· ·
Article Categories:
reddit

Leave a Reply