Skip to content

Client.OptionsReader() returns a struct ClientOptionsReader instead of interface which make mocking impossible #661

Closed
@avmunm

Description

@avmunm

Hi,

Client.OptionsReader returns this object.

// ClientOptionsReader provides an interface for reading ClientOptions after the client has been initialized.
type ClientOptionsReader struct {
	options *ClientOptions
}

When trying to mock the Client and using the options reader in my applications I get a nilpointer, as I can only return an empty struct of ClientOptionsReader. I think it would be better if ClientOptionsReader was an interface, so I could also mock it.
Unless I am missing something else I could do?
Going from this:

func NewMockClient() mqtt.Client {
	return &mockMQTTClient{}
}

type mockMQTTClient struct {
	subscriptions map[string]byte
}

func (c *mockMQTTClient) OptionsReader() mqtt.ClientOptionsReader {
	return mqtt.ClientOptionsReader{}
}

To this:

func NewMockClient() mqtt.Client {
	return &mockMQTTClient{}
}

type mockMQTTClient struct {}

func (c *mockMQTTClient) OptionsReader() mqtt.ClientOptionsReader {
	return &mockOptionsReader
}

type mockOptionsReader struct {}

func (r *mockOptionsReader) ResumeSubs() bool {
	return false
}
....

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions