mirror of
https://github.com/stretchr/testify.git
synced 2025-05-31 11:42:44 +00:00
Adding logging when mock assertions fails
This commit is contained in:
parent
a726187e31
commit
be8372ae8e
@ -389,6 +389,7 @@ func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool {
|
|||||||
}
|
}
|
||||||
m := obj.(assertExpectationser)
|
m := obj.(assertExpectationser)
|
||||||
if !m.AssertExpectations(t) {
|
if !m.AssertExpectations(t) {
|
||||||
|
t.Logf("Expectations didn't match for Mock: %+v", reflect.TypeOf(m))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package mock
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -1328,6 +1329,37 @@ func (s *timer) GetTime(i int) string {
|
|||||||
return s.Called(i).Get(0).(string)
|
return s.Called(i).Get(0).(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type tCustomLogger struct {
|
||||||
|
*testing.T
|
||||||
|
logs []string
|
||||||
|
errs []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tc *tCustomLogger) Logf(format string, args ...interface{}) {
|
||||||
|
tc.T.Logf(format, args...)
|
||||||
|
tc.logs = append(tc.logs, fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tc *tCustomLogger) Errorf(format string, args ...interface{}) {
|
||||||
|
tc.errs = append(tc.errs, fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tc *tCustomLogger) FailNow() {}
|
||||||
|
|
||||||
|
func TestLoggingAssertExpectations(t *testing.T) {
|
||||||
|
m := new(timer)
|
||||||
|
m.On("GetTime", 0).Return("")
|
||||||
|
tcl := &tCustomLogger{t, []string{}, []string{}}
|
||||||
|
|
||||||
|
AssertExpectationsForObjects(tcl, m, new(TestExampleImplementation))
|
||||||
|
|
||||||
|
require.Equal(t, 1, len(tcl.errs))
|
||||||
|
assert.Regexp(t, regexp.MustCompile("(?s)FAIL: 0 out of 1 expectation\\(s\\) were met.*The code you are testing needs to make 1 more call\\(s\\).*"), tcl.errs[0])
|
||||||
|
require.Equal(t, 2, len(tcl.logs))
|
||||||
|
assert.Regexp(t, regexp.MustCompile("(?s)FAIL:\tGetTime\\(int\\).*"), tcl.logs[0])
|
||||||
|
require.Equal(t, "Expectations didn't match for Mock: *mock.timer", tcl.logs[1])
|
||||||
|
}
|
||||||
|
|
||||||
func TestAfterTotalWaitTimeWhileExecution(t *testing.T) {
|
func TestAfterTotalWaitTimeWhileExecution(t *testing.T) {
|
||||||
waitDuration := 1
|
waitDuration := 1
|
||||||
total, waitMs := 5, time.Millisecond*time.Duration(waitDuration)
|
total, waitMs := 5, time.Millisecond*time.Duration(waitDuration)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user