Replace bare *testing.T with a mockable interface

pull/977/head
Eugene Kim 6 years ago
parent 8d2ce0011a
commit df410425ea
  1. 49
      internal/utils/mock/testing.go
  2. 12
      internal/utils/testing.go

@ -0,0 +1,49 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: testing.go
// Package mock_utils is a generated GoMock package.
package mock_utils
import (
gomock "github.com/golang/mock/gomock"
reflect "reflect"
)
// MockTestLogger is a mock of TestLogger interface
type MockTestLogger struct {
ctrl *gomock.Controller
recorder *MockTestLoggerMockRecorder
}
// MockTestLoggerMockRecorder is the mock recorder for MockTestLogger
type MockTestLoggerMockRecorder struct {
mock *MockTestLogger
}
// NewMockTestLogger creates a new mock instance
func NewMockTestLogger(ctrl *gomock.Controller) *MockTestLogger {
mock := &MockTestLogger{ctrl: ctrl}
mock.recorder = &MockTestLoggerMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockTestLogger) EXPECT() *MockTestLoggerMockRecorder {
return m.recorder
}
// Log mocks base method
func (m *MockTestLogger) Log(args ...interface{}) {
m.ctrl.T.Helper()
varargs := []interface{}{}
for _, a := range args {
varargs = append(varargs, a)
}
m.ctrl.Call(m, "Log", varargs...)
}
// Log indicates an expected call of Log
func (mr *MockTestLoggerMockRecorder) Log(args ...interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Log", reflect.TypeOf((*MockTestLogger)(nil).Log), args...)
}

@ -3,11 +3,12 @@ package utils
import (
"fmt"
"strings"
"testing"
"github.com/ethereum/go-ethereum/log"
)
//go:generate mockgen -source=testing.go -destination=mock/testing.go
// TestLogRedirector is a log15-compatible log handler that logs to the testing
// object. It is useful when debugging, because it surfaces application log
// messages – such as ones emitted by the SUT – which are otherwise discarded
@ -29,14 +30,19 @@ import (
type TestLogRedirector struct {
l log.Logger
h log.Handler
t *testing.T
t TestLogger
}
// TestLogger is the logging interface implemented by testing.T.
type TestLogger interface {
Log(args ...interface{})
}
// NewTestLogRedirector returns a new testing log redirector.
// The given logger's handler is saved and replaced by the receiver.
// Caller shall ensure Close() is called when the redirector is no longer
// needed.
func NewTestLogRedirector(l log.Logger, t *testing.T) *TestLogRedirector {
func NewTestLogRedirector(l log.Logger, t TestLogger) *TestLogRedirector {
r := &TestLogRedirector{l: l, h: l.GetHandler(), t: t}
l.SetHandler(r)
return r

Loading…
Cancel
Save