Open wangtengda0310 opened 1 month ago
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.
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
)
there is a RETURNS_DEEP_STUBS
in mockito
https://github.com/mockito/mockito/blob/52fdc52c874ec309116288ba8b1617764e2b24a4/src/main/java/org/mockito/Mockito.java#L1827
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.
pegomock.When(mock.Foo().Bar()).ThenReturn("how to mock Bar() conveniently?")