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

is there an support for RETURNS_DEEP_STUBS #128

Open
wangtengda0310 opened this issue Aug 31, 2024 · 4 comments
Open

is there an support for RETURNS_DEEP_STUBS #128

wangtengda0310 opened this issue Aug 31, 2024 · 4 comments

Comments

@wangtengda0310
Copy link

wangtengda0310 commented Aug 31, 2024

pegomock.When(mock.Foo().Bar()).ThenReturn("how to mock Bar() conveniently?")

@petergtz
Copy link
Owner

petergtz commented Sep 1, 2024

Hi @wangtengda0310, could you provide more details? E.g. could you specify the Interfaces and what you expect from the methods to return exactly. Thanks.

@wangtengda0310
Copy link
Author

image

interface/interface.go

package _interface

type Foo interface {
	Foo() Bar
}

type Bar interface {
	Bar() string
}

pegomock/pegomock_test.go

package pegomock

import (
	"github.com/petergtz/pegomock/v4"
	"github.com/stretchr/testify/assert"
	"testing"
)

//go:generate pegomock generate --package=pegomock myproj/interface Foo
//go:generate pegomock generate --package=pegomock myproj/interface Bar

func TestFoobar(t *testing.T) {
	t.Run("this is ugly", func(t *testing.T) {
		foo := NewMockFoo()
		bar := NewMockBar()
		pegomock.When(foo.Foo()).ThenReturn(bar)
		pegomock.When(bar.Bar()).ThenReturn("baz")
		assert.Equal(t, "baz", foo.Foo().Bar())
	})
	t.Run("this is pretty but nil pointer error", func(t *testing.T) {
		foo := NewMockFoo()
		pegomock.When(foo.Foo().Bar()).ThenReturn("baz")
		assert.Equal(t, "baz", foo.Foo().Bar())
	})
}

go.mod

module myproj

go 1.22.5

require (
	github.com/petergtz/pegomock/v4 v4.1.0
	github.com/stretchr/testify v1.9.0
)

require (
	github.com/davecgh/go-spew v1.1.1 // indirect
	github.com/gopherjs/gopherjs v1.17.2 // indirect
	github.com/jtolds/gls v4.20.0+incompatible // indirect
	github.com/onsi/gomega v1.27.6 // indirect
	github.com/pmezard/go-difflib v1.0.0 // indirect
	github.com/smarty/assertions v1.15.0 // indirect
	gopkg.in/yaml.v3 v3.0.1 // indirect
)

@wangtengda0310
Copy link
Author

@petergtz
Copy link
Owner

petergtz commented Sep 2, 2024

Thanks for clarifying. Pegomock doesn't support that feature. I guess it could be implemented, but it's not trivial to do so. As the mockito documentation says, this pattern should be used in very rare cases, such as legacy code. If you still need it and are willing to contribute, I'd be happy to merge a PR.

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

2 participants