Create convenient method to mock the timeduration.

This commit is contained in:
Mariano Cano 2019-07-31 12:53:03 -07:00
parent 4c1a11c1bc
commit c17375a10a

View file

@ -6,6 +6,17 @@ import (
"time" "time"
) )
func mockNow() (time.Time, func()) {
tm := time.Unix(1584198566, 535897000).UTC()
nowFn := now
now = func() time.Time {
return tm
}
return tm, func() {
now = nowFn
}
}
func TestNewTimeDuration(t *testing.T) { func TestNewTimeDuration(t *testing.T) {
tm := time.Unix(1584198566, 535897000).UTC() tm := time.Unix(1584198566, 535897000).UTC()
type args struct { type args struct {
@ -186,15 +197,8 @@ func TestTimeDuration_UnmarshalJSON(t *testing.T) {
} }
func TestTimeDuration_Time(t *testing.T) { func TestTimeDuration_Time(t *testing.T) {
nowFn := now tm, fn := mockNow()
defer func() { defer fn()
now = nowFn
now()
}()
tm := time.Unix(1584198566, 535897000).UTC()
now = func() time.Time {
return tm
}
tests := []struct { tests := []struct {
name string name string
timeDuration *TimeDuration timeDuration *TimeDuration
@ -211,22 +215,14 @@ func TestTimeDuration_Time(t *testing.T) {
got := tt.timeDuration.Time() got := tt.timeDuration.Time()
if !reflect.DeepEqual(got, tt.want) { if !reflect.DeepEqual(got, tt.want) {
t.Errorf("TimeDuration.Time() = %v, want %v", got, tt.want) t.Errorf("TimeDuration.Time() = %v, want %v", got, tt.want)
} }
}) })
} }
} }
func TestTimeDuration_Unix(t *testing.T) { func TestTimeDuration_Unix(t *testing.T) {
nowFn := now tm, fn := mockNow()
defer func() { defer fn()
now = nowFn
now()
}()
tm := time.Unix(1584198566, 535897000).UTC()
now = func() time.Time {
return tm
}
tests := []struct { tests := []struct {
name string name string
timeDuration *TimeDuration timeDuration *TimeDuration
@ -250,15 +246,8 @@ func TestTimeDuration_Unix(t *testing.T) {
} }
func TestTimeDuration_String(t *testing.T) { func TestTimeDuration_String(t *testing.T) {
nowFn := now tm, fn := mockNow()
defer func() { defer fn()
now = nowFn
now()
}()
tm := time.Unix(1584198566, 535897000).UTC()
now = func() time.Time {
return tm
}
type fields struct { type fields struct {
t time.Time t time.Time
d time.Duration d time.Duration