Closed yonesko closed 5 months ago
I don't understand your issue. Please post a self-contained example that reproduces your issue using this template: https://go.dev/play/p/AsRNcxCRdWx
I have updated the first comment
The Testify API is very comprehensive. Have you tried the other Equal
functions? Please expand your test code to show the issue you have with the other functions.
By the way, this isn't a support forum. Issues are for reporting bugs or discussing enhancements. There are more appropriate places for asking for help.
This is not support^ this is bug
similar issue as #1461. This should be resolved whenever a new release is cut.
i have met with this too, please release a new package asap
similar issue as #1461. This should be resolved whenever a new release is cut.
It's a little different. Here T
time is a field in a struct, meaning the equality comparison defaults to reflect.DeepEqual
.
New release will not fix this issue.
Testify is correct, they are not equal. The behaviour of actual is demonstrably different to the behaviour of expected.
If you only care that they have the same time, then use WithinDuration:
package main
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type A struct {
T time.Time
}
func Test(t *testing.T) {
now := time.Now()
a := A{T: now}
marshal, _ := json.Marshal(a)
var aParsed A
json.Unmarshal(marshal, &aParsed)
assert.WithinDuration(t, a.T, aParsed.T, 0)
}
How to compare not times but a structure with time fields ?
How to compare not times but a structure with time fields ?
You'll need to either test the fields individually or write an equality test helper function for your struct, for what you consider to be equal. time.Time values are not the only examply where this becomes necessary, another common one is slice type fields where you only care about the elements matching and not the order. The fields literally aren't equal but you can't use EqualElements on a struct.
Another option may be to use time.Now().Truncate(0)
as your test input, this strips the wall time.
I'm afraid we can't call this a bug because the behaviour of Equal is as documented according to the package docs and the Go spec, and to change the bahaviour would be a breaking change. What if someone's test depends on the wall time difference between time.Time values?
If you can reason that this change should be made, or you can propose a good design for a compatible change that isn't assert.SortOfEqual
please comment and I will re-open the issue.
Have an annoying issue comparing structs with time:
Output: