golang / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
9.26k stars 608 forks source link

Pass if struct contains minimal required parameters #694

Closed tsuz closed 1 year ago

tsuz commented 1 year ago

Requested feature A clear description of the desired feature and an example of how it would be used.

Feature Check if a struct A contains at least some parameters of A. Using the example below,

type A struct {
  Foo string
  Bar string
}

func (r *repo) Method(b A) {}

I would like to check if theMethod()'s argument contains at least some required parameters of A.

Currently, the below fails because the two instances contain different parameters.

// test.go

minimal := A{ Foo: "foo1" }
m.EXPECT().Method(minimal).Return()

// src.go
allparams := A{  Foo: "foo1", Bar: "bar1" }
Method(allparams)

Use Case

I have a model that generates a unique ID (for example, "uuid.V4()") that is not predictable when the test is ran. I currently work around this by returning an error. For example,

m.EXPECT().
  Method(gomock.Any()).
  Return(func(v interface{}) {
    s, ok := v.(A)
    if (s.Foo != "foo1") {
      t.Fatal(err)
    }
  })

If there is a simpler way, I'd like to know.

Why the feature is needed A clear description of how this feature is not served by existing functionality in gomock.

I could not find the method that does. (correct me if I'm wrong)

(Optional) Proposed solution A clear description of a proposed method for adding this feature to gomock.

(If there is not a better workaround than what I have now, I can come up with something)

tsuz commented 1 year ago

Resolved by not generating a UUID within the module.