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

What is the best way to mock carbon in unit testing? #195

Closed
matt9mg opened this issue Nov 3, 2023 · 11 comments
Closed

What is the best way to mock carbon in unit testing? #195

matt9mg opened this issue Nov 3, 2023 · 11 comments
Labels
Question Indicates that an issue, pull request, or discussion needs more information

Comments

@matt9mg
Copy link

matt9mg commented Nov 3, 2023

Hi,

I'm wondering what the best way to mock carbon as I cannot see an interface to aid with testing a custom service.

Thanks,

@matt9mg matt9mg added the Question Indicates that an issue, pull request, or discussion needs more information label Nov 3, 2023
@gouguoyin
Copy link
Member

@matt9mg
Copy link
Author

matt9mg commented Nov 5, 2023

oh nice thank you, I have a separate question say I was to inject carbon into a service struct i.e.

func main() {
  lang := carbon.NewLanguage()
  lang.SetResources(map[string]string{
  "before": "Expires in %s",
  "after":  "Expired %s ago",
  })
  
  c := carbon.SetLanguage(lang)

  ms := services.NewMyService(&c)
  diff := ms.FromNowHumanReadable(...)
}
type MysServiceInterface interface {
   FromNowHumanReadable(date time.time) string
}

type MyService struct {
   c *carbon.Carbon
}

func NewMyService(c *carbon.Carbon) MysServiceInterface {
   return &MyService{
      c: c,
   }
}

func (d *Date) FromNowHumanReadable(date time.Time) string {
	endDate := d.carbon.CreateFromTimestamp(date.Unix())

	return d.carbon.Now().DiffForHumans(endDate)
}

In this scenario, i have a wrapper helper to always get the diff from now, how do I mock carbon in a case where I have a single language which will save having to append the language string each call.

@gouguoyin
Copy link
Member

Language locale must be set when rewriting some translation resources,if rewriting all resources, it is not necessary.

lang := carbon.NewLanguage()
lang.SetLocale("en")
lang.SetResources(map[string]string{
    "before": "Expires in %s",
    "after":  "Expired %s ago",
})

@matt9mg
Copy link
Author

matt9mg commented Nov 6, 2023

I appreciate what you are saying here 👍

@matt9mg matt9mg closed this as completed Nov 6, 2023
@Issues-translate-bot
Copy link
Collaborator

The issue body's language is not English, translate it automatically, please use English next time. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


I appreciate what you are saying here 👍

@fikurimax
Copy link

I am testing a web service and trying to mock the time using .SetTestNow() but in the handler/controller the time is not mocked

@gouguoyin
Copy link
Member

I am testing a web service and trying to mock the time using .SetTestNow() but in the handler/controller the time is not mocked

Show sample code.

@gouguoyin gouguoyin reopened this Nov 23, 2023
@fikurimax
Copy link

image
The handler lives in different package than the test file

@gouguoyin
Copy link
Member

gouguoyin commented Nov 23, 2023

image The handler lives in different package than the test file

Carbon is not a pointer, so after SetTestNow, the value of carbon cannot be changed.

@gouguoyin
Copy link
Member

c := carbon.SetTimezone(carbon.UTC)

c.Now().ToDateString() // 2023-12-27
c.IsSetTestNow() // false

c.SetTestNow(carbon.Parse("2020-08-05"))
c.Now().ToDateString() // 2020-08-05
c.Now().IsSetTestNow() // true

c.UnSetTestNow()
c.Now().ToDateString() // 2023-12-2
c.Now().IsSetTestNow() // false

@fikurimax
Copy link

I thought we could do globally without creating an object like

carbon.SetTestNow(carbon.Parse("2020-12-12"))

And wherever i called

carbon.Now()

It'll give 2020-12-12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Indicates that an issue, pull request, or discussion needs more information
Development

No branches or pull requests

4 participants