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

Ignore fields using struct tags #843

Open
hiimoliverwang opened this issue Nov 12, 2019 · 3 comments
Open

Ignore fields using struct tags #843

hiimoliverwang opened this issue Nov 12, 2019 · 3 comments

Comments

@hiimoliverwang
Copy link

Hello,

Is there a way to ignore certain fields when asserting equal or as the argument of .On?

e.g.

type A struct {
  Name string
  UpdatedAt time.Time `mock: "ignore"`
}

expected := A{
    Name: "Expected Name"
}
mocks.MockController.On("Update", expected).Return(whatever)
...

We don't really care about the UpdateAt time, and this would also apply to random UUID's etc.
Otherwise we would have to assert each sub field in order for the test to pass.

@boyan-soubachov
Copy link
Collaborator

Not that I am aware of, would it make sense in your use case to explicitly assert the fields (if the include list is small) or conversely shallow-copy the struct and remove the ones you don't want to compare (if the exclude list is small)?

@Khyme
Copy link

Khyme commented Sep 27, 2021

Hello. Is there any news on this? I have the issue described, and I cannot use the suggested ways of @boyan-soubachov because it's intricated controllers so I don't get to manipulate the intermediate result used by the mock.

@brackendawson
Copy link
Collaborator

I think this could be a good approach for the issues people face with comparing nested time.Time objects and unsorted array types? Struct tags could override the comparison used, for example:

my_code.go

...

type MyAmazingThing struct {
    ID string
    Created time.Time
    Documents []string
    DeleteFn func() error
}

...

my_code_test.go

...

type myAmazingThingEqual struct {
    ID string
    Created time.Time     // `testify:"WithinDuration,0"`
    Documents []string    // `testify:"ElementsMatch"`
    DeleteFn func() error // `testify:"ignore"`
}

assert.Equal(t, expected, myAmazingThingEqual(actual))

...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants