Closed gmhafiz closed 5 months ago
Have you read the api docs of sqlmock before asking?
@gmhafiz check Customize SQL query matching
chapter of https://pkg.go.dev/github.com/DATA-DOG/go-sqlmock?utm_source=godoc
Add this into your test file
type AnyTime struct{}
func (a AnyTime) Match(v driver.Value) bool {
_, ok := v.(time.Time)
return ok
}
and replace your data time.now() with AnyTime{}
mock.ExpectExec("UPDATE books set title").
WithArgs(mockBook.Title, mockBook.Description, mockBook.PublishedDate, mockBook.ImageURL.String, AnyTime{}, mockBook.BookID).
WillReturnResult(sqlmock.NewErrorResult(nil))
I have a function with
that I tried to unit test with
The fails obviously because the value of
time.Now()
in the test and in the implementation are different.How do you write unit a test when the implementation has a value that will change like
time.Now()
or somehow mocktime.Now()
For reference,
book
struct