From 20483034d01828529e5314f3d6a9334d94bd5b45 Mon Sep 17 00:00:00 2001 From: Fedor Korotkiy Date: Thu, 19 Mar 2020 16:23:32 +0300 Subject: [PATCH] Update lecture --- lectures/04-testing/gomock/example.go | 11 +++++ lectures/04-testing/gomock/example_test.go | 25 ++++++++++++ lectures/04-testing/gomock/mock.go | 47 ++++++++++++++++++++++ lectures/04-testing/httptest/main.go | 26 ++++++++++++ lectures/04-testing/lecture.slide | 13 ++++++ 5 files changed, 122 insertions(+) create mode 100644 lectures/04-testing/gomock/example.go create mode 100644 lectures/04-testing/gomock/example_test.go create mode 100644 lectures/04-testing/gomock/mock.go create mode 100644 lectures/04-testing/httptest/main.go diff --git a/lectures/04-testing/gomock/example.go b/lectures/04-testing/gomock/example.go new file mode 100644 index 0000000..d316a9c --- /dev/null +++ b/lectures/04-testing/gomock/example.go @@ -0,0 +1,11 @@ +package example + +//go:generate mockgen -package example -destination mock.go . Foo + +type Foo interface { + Bar(x int) int +} + +func SUT(f Foo) { + // ... +} diff --git a/lectures/04-testing/gomock/example_test.go b/lectures/04-testing/gomock/example_test.go new file mode 100644 index 0000000..195bbbf --- /dev/null +++ b/lectures/04-testing/gomock/example_test.go @@ -0,0 +1,25 @@ +package example + +import ( + "testing" + + gomock "github.com/golang/mock/gomock" +) + +func TestFoo(t *testing.T) { + ctrl := gomock.NewController(t) + + // Assert that Bar() is invoked. + defer ctrl.Finish() + + m := NewMockFoo(ctrl) + + // Asserts that the first and only call to Bar() is passed 99. + // Anything else will fail. + m. + EXPECT(). + Bar(gomock.Eq(99)). + Return(101) + + SUT(m) +} diff --git a/lectures/04-testing/gomock/mock.go b/lectures/04-testing/gomock/mock.go new file mode 100644 index 0000000..b6d5ac2 --- /dev/null +++ b/lectures/04-testing/gomock/mock.go @@ -0,0 +1,47 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: gitlab.com/slon/shad-go/lectures/04-testing/gomock (interfaces: Foo) + +// Package example is a generated GoMock package. +package example + +import ( + gomock "github.com/golang/mock/gomock" + reflect "reflect" +) + +// MockFoo is a mock of Foo interface +type MockFoo struct { + ctrl *gomock.Controller + recorder *MockFooMockRecorder +} + +// MockFooMockRecorder is the mock recorder for MockFoo +type MockFooMockRecorder struct { + mock *MockFoo +} + +// NewMockFoo creates a new mock instance +func NewMockFoo(ctrl *gomock.Controller) *MockFoo { + mock := &MockFoo{ctrl: ctrl} + mock.recorder = &MockFooMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockFoo) EXPECT() *MockFooMockRecorder { + return m.recorder +} + +// Bar mocks base method +func (m *MockFoo) Bar(arg0 int) int { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Bar", arg0) + ret0, _ := ret[0].(int) + return ret0 +} + +// Bar indicates an expected call of Bar +func (mr *MockFooMockRecorder) Bar(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Bar", reflect.TypeOf((*MockFoo)(nil).Bar), arg0) +} diff --git a/lectures/04-testing/httptest/main.go b/lectures/04-testing/httptest/main.go new file mode 100644 index 0000000..4179878 --- /dev/null +++ b/lectures/04-testing/httptest/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "io" + "io/ioutil" + "net/http" + "net/http/httptest" +) + +func main() { + handler := func(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "Hello World!") + } + + req := httptest.NewRequest("GET", "http://example.com/foo", nil) + w := httptest.NewRecorder() + handler(w, req) + + resp := w.Result() + body, _ := ioutil.ReadAll(resp.Body) + + fmt.Println(resp.StatusCode) + fmt.Println(resp.Header.Get("Content-Type")) + fmt.Println(string(body)) +} diff --git a/lectures/04-testing/lecture.slide b/lectures/04-testing/lecture.slide index 19a663c..0f0555a 100644 --- a/lectures/04-testing/lecture.slide +++ b/lectures/04-testing/lecture.slide @@ -486,4 +486,17 @@ Good example .play mocks/mocks_test.go /func/,/^}/ +* gomock +.play gomock/example.go + +- Запуск `go`generate`.` создаст файл `mock.go` +- Хорошая идея - класть `mock`-и в отдельный пакет. + +* gomock + +.play gomock/example_test.go + +* httptest + +.play httptest/main.go /func/,/^}/