Documentation
¶
Index ¶
- Variables
- type BytesExpectation
- type Expectation
- func (expect *Expectation) Got() string
- func (expect *Expectation) ReturnsError(err error) *Expectation
- func (expect *Expectation) Wanted() string
- func (expect *Expectation) WithHeader(k string) *Expectation
- func (expect *Expectation) WithHeaderValue(k, v string) *Expectation
- func (expect *Expectation) WithHeaders(v map[string]string) *Expectation
- func (expect *Expectation) WithKey() BytesExpectation
- func (expect *Expectation) WithValue() BytesExpectation
- type Producer
Constants ¶
This section is empty.
Variables ¶
var ErrTopicNotSet = errors.New("topic not set")
Functions ¶
This section is empty.
Types ¶
type BytesExpectation ¶
type Expectation ¶
type Expectation struct {
// contains filtered or unexported fields
}
func (*Expectation) Got ¶
func (expect *Expectation) Got() string
func (*Expectation) ReturnsError ¶
func (expect *Expectation) ReturnsError(err error) *Expectation
func (*Expectation) Wanted ¶
func (expect *Expectation) Wanted() string
func (*Expectation) WithHeader ¶
func (expect *Expectation) WithHeader(k string) *Expectation
func (*Expectation) WithHeaderValue ¶
func (expect *Expectation) WithHeaderValue(k, v string) *Expectation
func (*Expectation) WithHeaders ¶
func (expect *Expectation) WithHeaders(v map[string]string) *Expectation
WithHeaders specifies a complete set of expected headers.
A message must be produced with headers matching the specified map in order for the expectation to be met. Ordering of keys is not significant, but every key/value pair in the specified map must be present in the message headers.
If WithHeaders is specified, WithHeader cannot also be specified.
If an empty map is specified, the message must be produced with no headers in order for the expectation to be met. This is equivalent to setting a WithNoHeaders() expectation.
func (*Expectation) WithKey ¶
func (expect *Expectation) WithKey() BytesExpectation
WithKey establishes a message Key expectation. Returns a BytesExpectation that may be used to set a specific expected key value.
Examples:
WithKey() - any non-empty key WithKey().Empty() - the key must be empty WithKey().Value(v) - the key must be exactly v WithKey().AsJson(v) - the key must be exactly json.Marshal(v) WithKey().AsString(v) - the key must be exactly []byte(v)
func (*Expectation) WithValue ¶
func (expect *Expectation) WithValue() BytesExpectation
WithValue establishes a message Value expectation. Returns a BytesExpectation that may be used to set a specific expected Value.
Examples:
WithValue() - any non-empty Value WithValue().Empty() - the Value must be empty WithValue().Value(v) - the Value must be exactly v WithValue().AsJson(v) - the Value must be exactly json.Marshal(v) WithValue().AsString(v) - the Value must be exactly []byte(v)
type Producer ¶
type Producer[T comparable] struct { // contains filtered or unexported fields }
Producer provides a mock implementation of kafka.Producer.
Since a Producer provides only a MustProduce() method, all expectations start with an expectation that some message will be produced to a particular topic.
The details of the expected message may then optionally be specified using a fluent api. If no details are specified, then any message to the topic will satisfy the expectation. Any details specified must all match those of the message produced for the expectation to be met:
Example: expecting any message to be produced to a topic:
p := &mock.Producer{} p.Expect("topic")
Example: expecting a message with a particular Header:
p := &mock.Producer{} p.Expect("topic"). WithHeader("header.key", "value")
If multiple messages are expected, the expectations are met only if messages are produced which meet the specified expectations in the order in which the expectations were set.
Example: expecting two messages to be produced where the first has a specific key and the second could be any message produced to a different topic:
p := &mock.Producer{} p.Expect("topic.a"). WithKey(customerId) p.Expect("topic.b")
If testing the behaviour of higher level code in response to producer errors, you may set an expectation that an attempt to produce a message will return an error.
Example: mocking a producer error:
p := &mock.Producer{} p.Expect("topic").ReturnsError(errors.New("producer error"))
To test whether expectations were met call ExpectationsMet() after the code under test has completed. If any expectations were not met false is returned with an error that describes the unmet expectations. If all expectations were met, true is returned with a nil error.
The bool indicator is used to control test flow and provide your own test failure report, ignoring the error:
if met, _ := p.ExpectationsMet(); !met { t.Error("did not produce 'completed' event") }
or you can ignore the bool indicator and simply report a non-nil error:
if _, err := p.ExpectationsMet(); err != nil { t.Error(err.Error()) }
To re-use a mock.Producer in multiple executions of a test (e.g. when using a data-driven test), the Reset() method may be used to reset the mock.Producer to its initial state, clearing all expectations.
func (*Producer[T]) Expect ¶
func (p *Producer[T]) Expect(topic T) *Expectation
Expect sets an expectation that a message will be produced to the specified topic. The expectation may be further specified using the fluent API provided by the returned Expectation.
func (*Producer[T]) ExpectationsWereMet ¶
ExpectationsMet checks whether all expectations set on the Producer were met.
func (*Producer[T]) MustProduce ¶
func (p *Producer[T]) MustProduce(ctx context.Context, msg kafka.Message) (*kafka.TopicPartition, error)
MustProduce produces a message to the topic specified in the expectation.