That didn't work. It turned out that the slice elements are values and their GetName method is only defined for pointers. Go can call it anyway, but Gomega can't (or doesn't). Perhaps it should also support that?
Here's a reproducer:
package gomegatest
import (
"testing"
"github.com/onsi/gomega"
)
type pointerobject struct {
name string
}
func (p *pointerobject) GetName() string {
return p.name
}
type valueobject struct {
name string
}
func (v valueobject) GetName() string {
return v.name
}
type object interface {
GetName() string
}
var _ object = valueobject{}
// Only pointer implements object interface, not value.
var _ object = &pointerobject{}
// var _ object = pointerobject{}
func TestConsistsOf(t *testing.T) {
g := gomega.NewGomegaWithT(t)
e := gomega.ConsistOf(gomega.HaveField("GetName()", gomega.Equal("A")), gomega.HaveField("GetName()", gomega.Equal("B")))
g.Expect([]valueobject{{"A"}, {"B"}}).To(e) // works
g.Expect([]pointerobject{{"A"}, {"B"}}).To(e) // fails
}
I recently suggested replacing these checks with
ConsistOf + HaveField("GetName()", Equal
:=>
That didn't work. It turned out that the slice elements are values and their
GetName
method is only defined for pointers. Go can call it anyway, but Gomega can't (or doesn't). Perhaps it should also support that?Here's a reproducer: