Skip to content

Instantly share code, notes, and snippets.

@yukihir0
Last active January 16, 2022 08:47
Show Gist options
  • Save yukihir0/8b58666582edf78b6a8c to your computer and use it in GitHub Desktop.
Save yukihir0/8b58666582edf78b6a8c to your computer and use it in GitHub Desktop.
GolangでHTTPサーバのモック使ってテストを実行する。
package main
import (
"github.com/jarcoal/httpmock"
"io/ioutil"
"net/http"
"testing"
)
func TestHttpMockSample(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(
"GET",
"http://www.google.co.jp",
httpmock.NewStringResponder(200, "this is a mock."))
res, _ := http.Get("http://www.google.co.jp")
body, _ := ioutil.ReadAll(res.Body)
defer res.Body.Close()
expected := "this is a mock."
ret := string(body)
if ret != expected {
t.Errorf("expected %s, but got %s\n", expected, ret)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment