From c17375a10a9b475a3ddc84e015e3cecc85de33fa Mon Sep 17 00:00:00 2001 From: Mariano Cano Date: Wed, 31 Jul 2019 12:53:03 -0700 Subject: [PATCH] Create convenient method to mock the timeduration. --- authority/provisioner/timeduration_test.go | 45 ++++++++-------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/authority/provisioner/timeduration_test.go b/authority/provisioner/timeduration_test.go index 1def427c..65bd6f96 100644 --- a/authority/provisioner/timeduration_test.go +++ b/authority/provisioner/timeduration_test.go @@ -6,6 +6,17 @@ import ( "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) { tm := time.Unix(1584198566, 535897000).UTC() type args struct { @@ -186,15 +197,8 @@ func TestTimeDuration_UnmarshalJSON(t *testing.T) { } func TestTimeDuration_Time(t *testing.T) { - nowFn := now - defer func() { - now = nowFn - now() - }() - tm := time.Unix(1584198566, 535897000).UTC() - now = func() time.Time { - return tm - } + tm, fn := mockNow() + defer fn() tests := []struct { name string timeDuration *TimeDuration @@ -211,22 +215,14 @@ func TestTimeDuration_Time(t *testing.T) { got := tt.timeDuration.Time() if !reflect.DeepEqual(got, tt.want) { t.Errorf("TimeDuration.Time() = %v, want %v", got, tt.want) - } }) } } func TestTimeDuration_Unix(t *testing.T) { - nowFn := now - defer func() { - now = nowFn - now() - }() - tm := time.Unix(1584198566, 535897000).UTC() - now = func() time.Time { - return tm - } + tm, fn := mockNow() + defer fn() tests := []struct { name string timeDuration *TimeDuration @@ -250,15 +246,8 @@ func TestTimeDuration_Unix(t *testing.T) { } func TestTimeDuration_String(t *testing.T) { - nowFn := now - defer func() { - now = nowFn - now() - }() - tm := time.Unix(1584198566, 535897000).UTC() - now = func() time.Time { - return tm - } + tm, fn := mockNow() + defer fn() type fields struct { t time.Time d time.Duration