Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Returning another mock from a mock #316

@hashcacher

Description

@hashcacher

I have a few interfaces to mimic some classes from consul (https://github.com/hashicorp/consul)

// consul_test.go
package consul

import "github.com/golang/mock/gomock"

func TestNew (t *testing.T) {
	ctrl := gomock.NewController(t)
	defer ctrl.Finish()

	mc := NewMockconsulClient(ctrl)
	mcc := NewMockconsulCatalog(ctrl)

	mc.
		EXPECT().
		Catalog().
		Return(mcc)

	consul, err := New(mc)
}
...
// consul.go
package consul

import "github.com/hashicorp/consul/api"

type consulClient interface {
	Catalog() *api.Catalog
}

type consulCatalog interface {
	Datacenters() ([]string, error)
}

func New(client consulClient) (*Consul, error) {
	catalog := c.client.Catalog()
	dcs, err := catalog.Datacenters()
        ...
}

Here's the error:

    consul_test.go:48: wrong type of argument 0 to Return for *consul.MockconsulClient.Catalog: *consul.MockconsulCatalog is not assignable to *api.Catalog [/Users/gregory/code/consul-go/pkg/consul/consul_test.go:47]

The error is referring to the Return(mcc)

Is there a workaround for this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions