Last active
March 12, 2019 03:04
-
-
Save draveness/6cdbf73bcc9184e963445ae97bcd6aa4 to your computer and use it in GitHub Desktop.
Mock Interface in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+-------------------------+ | |
| | | |
| | | |
+---------------------+ +----->| jekyll | | |
| | | | | | |
| | | +-------------------------+ | |
| Service +------+ | |
| | | |
| +------+ +-------------------------+ | |
+---------------------+ | | | | |
| | | | |
+----->| wordpress | | |
| | | |
+-------------------------+ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package service | |
type Post struct {} | |
type Blog interface { | |
ListPosts() []Post | |
} | |
type jekyll struct {} | |
func (b *jekyll) ListPosts() []Post { | |
return []Post{} | |
} | |
type wordpress struct{} | |
func (b *wordpress) ListPosts() []Post { | |
return []Post{} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package service | |
type service struct { | |
blog Blog | |
} | |
func NewService(b Blog) { | |
return &service{ | |
blog: b, | |
} | |
} | |
func (s *service) ListPosts() []Post { | |
return s.blog.ListPosts() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package service | |
func TestJekyllListPosts(t *testing.T) { | |
blog := &jekyll{} | |
assert.Equal(t, []Post{}, blog.ListPosts()) | |
} | |
func TestWordpressListPosts(t * testing.T) { | |
blog := &wordpress{} | |
assert.Equal(t, []Post{}, blog.ListPosts()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code generated by MockGen. DO NOT EDIT. | |
// Source: blog.go | |
// Package mock_service is a generated GoMock package. | |
package mock_service | |
import ( | |
gomock "github.com/golang/mock/gomock" | |
reflect "reflect" | |
) | |
// MockBlog is a mock of Blog interface | |
type MockBlog struct { | |
ctrl *gomock.Controller | |
recorder *MockBlogMockRecorder | |
} | |
// MockBlogMockRecorder is the mock recorder for MockBlog | |
type MockBlogMockRecorder struct { | |
mock *MockBlog | |
} | |
// NewMockBlog creates a new mock instance | |
func NewMockBlog(ctrl *gomock.Controller) *MockBlog { | |
mock := &MockBlog{ctrl: ctrl} | |
mock.recorder = &MockBlogMockRecorder{mock} | |
return mock | |
} | |
// EXPECT returns an object that allows the caller to indicate expected use | |
func (m *MockBlog) EXPECT() *MockBlogMockRecorder { | |
return m.recorder | |
} | |
// ListPosts mocks base method | |
func (m *MockBlog) ListPosts() []Post { | |
m.ctrl.T.Helper() | |
ret := m.ctrl.Call(m, "ListPosts") | |
ret0, _ := ret[0].([]Post) | |
return ret0 | |
} | |
// ListPosts indicates an expected call of ListPosts | |
func (mr *MockBlogMockRecorder) ListPosts() *gomock.Call { | |
mr.mock.ctrl.T.Helper() | |
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListPosts", reflect.TypeOf((*MockBlog)(nil).ListPosts)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package service | |
import ( | |
"github.com/golang/mock/gomock" | |
"github.com/stretchr/testify/assert" | |
"xxxx/mock_service" | |
) | |
func TestListPosts(t *testing.T) { | |
ctrl := gomock.NewController(t) | |
defer ctrl.Finish() | |
mockBlog := mock_service.NewMockBlog(ctrl) | |
mockBlog.EXPECT().ListPosts().Return([]Post{}) | |
service := NewService(mockBlog) | |
assert.Equal(t, []Post{}, service.ListPosts()) | |
} |
Author
draveness
commented
Mar 8, 2019
type Scheduler interface{}
type scheduler struct{}
func NewScheduler() Scheduler {
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment