Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make easier to test with time.Now as a config #49

Open
flibustenet opened this issue Aug 2, 2021 · 2 comments
Open

Make easier to test with time.Now as a config #49

flibustenet opened this issue Aug 2, 2021 · 2 comments
Assignees

Comments

@flibustenet
Copy link

Describe the feature

It's difficult to test function in main like now.BeginingOf... because it use time.Now()

I suggest that the function time.Now could be set global like
var WeekStartDay = time.Sunday

We could have
var TimeNowFunction = time.Now

now.BeginingOfDay() will become return With(TimeNowFunction()).BeginningOfDay()

Then when we start our test we just need to set
now.TimeNowFunction = func() time.Time { return time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local) }

Thanks to listen

@Akaame
Copy link
Contributor

Akaame commented Aug 21, 2021

It's difficult to test function in main like now.BeginingOf... because it use time.Now()

For testing purposes, one can just use the With free function which returns an instance of Now that has the same family of BeginningOf functions.

now.With(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local)).BeginningOfWeek()

Having said that, the ability the override such stuff exists in many Golang libraries via conventional OverrideFooFunc facilities, which seems like a good addition to this library but I do not see it bringing much of a value testing wise.

Cheers.

@flibustenet
Copy link
Author

My purpose is to can test a function that use now.BeginingOf..., not to use now inside a test.
For example we cannot add a test in this package for the function now.BeginningOfWeek(). I mean it's an usual issue to test a function that use the current datetime...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants