Comments (10)
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)
}
from testify.
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.
from testify.
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
from testify.
I have updated the first comment
from testify.
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.
from testify.
This is not support^ this is bug
from testify.
similar issue as #1461. This should be resolved whenever a new release is cut.
from testify.
i have met with this too, please release a new package asap
from testify.
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.
from testify.
How to compare not times but a structure with time fields ?
from testify.
Related Issues (20)
- Data race when mock is called with refernce to same object as expectation HOT 1
- assert fails and expects to dereference a reference HOT 1
- Add GoLang 1.22 to CI Testing Matrix for Package Compatibility Verification HOT 2
- error while importing github.com/stretchr/testify/suite: read C:\Program Files\Go\src\math\huge_test.go: unexpected NUL in input HOT 1
- AnythingOfType is marked deprecated on pkgsite HOT 1
- .On().Return() doesn't enforce expectation for the returned value HOT 1
- v1.9.0 breaks float comparisons with EqualValues HOT 6
- mock: cleanup argument matching API HOT 7
- Integer overflow in time.Time comparison
- EqualExportedValues can compare two slices HOT 2
- Regexp accepts interface{} HOT 3
- Drop outdated/abandoned dependencies HOT 3
- Proposal: Must-like utility HOT 3
- Proposal: assert.NotElementsMatch() HOT 4
- assert: invalid memory address or nil pointer dereference for assert.Error on darwin/arm64 HOT 1
- mock: Diff is prone to data races
- call.Unset() panics when call := mockObj.On("Foo", mock.AnythingOfType("foo")) HOT 1
- Proposal: assert.NoFieldIsEmpty HOT 13
- Feature request: add BeforeSetupTest and AfterTearDownTest HOT 3
- Misleading Error() docs HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testify.