bytedance / mockey

a simple and easy-to-use golang mock library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

interface mocking

ZaynJarvis opened this issue · comments

Is your feature request related to a problem? Please describe.

A quick guide for interface mocking is needed

Describe the solution you'd like

type Fooer interface{
	Foo(string) string
}

type A struct{}

func (a A) Foo(in string) string { return in }

func UseFoo(f Fooer) string {
        // something
        return f.Foo("")
}

func TestMockXXX(t *testing.T) {
	PatchConvey("TestMockXXX", t, func() {
                Mock(Fooer.Foo).Return("c").Build() // something like this is needed

		So(UseFoo(A{}), ShouldEqual, "c") // assert `Fooer.Foo` is mocked
	})
}

Describe alternatives you've considered

func TestMockXXX(t *testing.T) {
	PatchConvey("TestMockXXX", t, func() {
                MockInterface(Fooer.Foo).Return("c").Build() // something like this is needed

		So(UseFoo(A{}), ShouldEqual, "c") // assert `Fooer.Foo` is mocked
	})
}

Additional context

  • for now GetNestedMethod seems to work, but not elegant.
  • I know I can do interface insertion, but this Mock func is just so good, I want to use it the same way.

Mock(Fooer.Foo).Return("c").Build() means 'mock every implements of the Fooer interface', right?

yes that will be expected.

In case there are same interface in a structure for example

type A struct {
   X Fooer
   Y Fooer
}

Mock(A.X.Foo) may be a neat way.

Is your feature request related to a problem? Please describe.

A quick guide for interface mocking is needed

Describe the solution you'd like

type Fooer interface{
	Foo(string) string
}

type A struct{}

func (a A) Foo(in string) string { return in }

func UseFoo(f Fooer) string {
        // something
        return f.Foo("")
}

func TestMockXXX(t *testing.T) {
	PatchConvey("TestMockXXX", t, func() {
                Mock(Fooer.Foo).Return("c").Build() // something like this is needed

		So(UseFoo(A{}), ShouldEqual, "c") // assert `Fooer.Foo` is mocked
	})
}

Describe alternatives you've considered

func TestMockXXX(t *testing.T) {
	PatchConvey("TestMockXXX", t, func() {
                MockInterface(Fooer.Foo).Return("c").Build() // something like this is needed

		So(UseFoo(A{}), ShouldEqual, "c") // assert `Fooer.Foo` is mocked
	})
}

Additional context

  • for now GetNestedMethod seems to work, but not elegant.
  • I know I can do interface insertion, but this Mock func is just so good, I want to use it the same way.

can't agree more! and how is the feature going? @ycydsxy

+1