-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Support structs in assert.Contains() #1432
Comments
I would be willing to contribute the work for this, if there is interest from the testify team. |
I don't understand how your specification would fit your use case. UUID would be of string type and create/update timestamp would be Edit: ok, the expected struct would have the zero value for those fields so they would be skipped. |
I feel like this is a risky way to solve the problem. The addition of fields to the struct in the future of the package could nullify the assertion. Given you know what field it is going to be in, you should test that field. The proposed behaviour is very difficult to document and I don't think it represents the job of the Contains asserition. If terseness is what you are looking for, the solution that has been recommended many tiemes before is as simple: #843 (comment) just zero the unexpectable fields on your |
That depends a lot on one's testing philosophy. In general I agree that if you expect the system to behave a certain way, you should test that expectation. However I also follow the philosophy that backward-compatible changes should not break existing tests. Tests that violate this rule are called change detectors, and make developers lives miserable. Adding a new optional or computed field to an API is a backward-compatible change. I may want to update existing tests to add new assertions, or I may want to add new tests for that. The
I've written similar assertions in the past, and would contribute the documentation with the implementation.
Would you prefer if this lived in its own function, e.g. |
I don't think this is core functionality that we need to have (and maintain forever) in testify. The comparison could be published in a separate package. As @brackendawson said, there is a risk of misuse. As maintainers we can't take the load of questions coming. I'm sorry if this feels very conservative, but it really is because testify is very popular. |
It sounds like the testify team isn't interested in this as a feature. Closing. |
Often large objects cannot be compared using
assert.Equal()
, because they can contain extra data that cannot be predicted in a test. For example, servers may assign randomly generated UUIDs to created objects, or setCreatedAt
orUpdatedAt
fields at the server.When objects contain this extra data, often the test must compare each field one at a time. The
assert.Equal()
calls above become tedious to write when there are a lot of fields. They are also vulnerable to copy-paste errors, at least in my own experience.It would be helpful (and easier to read) if we could check all the predictable fields in one shot:
The way I envision this working is that when the object is a struct:
The text was updated successfully, but these errors were encountered: