Open
Description
Consider the following program:
type bar struct {}
func (b *bar) x() {}
func (b *bar) y() {}
type Foo struct {
bar *bar
}
func (f *Foo) baz() {
f.b.y()
}
Now assume we would like to test foo
and for this we'd want to mock bar
. The recommended way of doing this is to extract the way that bar
is used into an interface and use that in lieu of bar
in foo
.
type bar struct {}
func (b *bar) x() {}
func (b *bar) y() {}
type barI interface {
y()
}
type Foo struct {
bar barI
}
func (f *Foo) baz() {
f.b.y()
}
Note that only y()
was extracted.
Supporting such a code action in gopls would make life a lot easier for devs who write unit tests.
This is at least viable to implement for unexported members, as no new methods can be added in other packages which access this member.