Mocking your kafka consumer | Laravel Kafka              

 [ K Laravel Kafka ](/) [Docs](/docs) [Blog](https://laravelkafka.com/blog) [GitHub](https://github.com/mateusjunges/laravel-kafka) 

     Search ⌘K       [    Login with GitHub Login ](https://laravelkafka.com/oauth/github/redirect) 

    Docs for version         

   selected

 v3.0     

 v2.13     

 v2.12     

 v2.11     

 v2.10     

 v2.9     

 v2.8     

 v1.13   

- - [ Introduction ](/docs/v1.13/introduction)
    - [ Requirements ](/docs/v1.13/requirements)
    - [ Installation and Setup ](/docs/v1.13/installation-and-setup)
    - [ Questions and issues ](/docs/v1.13/questions-and-issues)
    - [ Changelog ](/docs/v1.13/changelog)
    - [ Upgrade Guide ](/docs/v1.13/upgrade-guide)
- Producing messages
    ------------------

    - [ Producing messages ](/docs/v1.13/producing-messages/producing-messages)
    - [ Configuring your kafka producer ](/docs/v1.13/producing-messages/configuring-producers)
    - [ Configuring message payload ](/docs/v1.13/producing-messages/configuring-message-payload)
    - [ Custom serializers ](/docs/v1.13/producing-messages/custom-serializers)
    - [ Publishing to kafka ](/docs/v1.13/producing-messages/publishing-to-kafka)
    - [ Producing message batch to kafka ](/docs/v1.13/producing-messages/producing-message-batch-to-kafka)
- Consuming messages
    ------------------

    - [ Creating a kafka consumer ](/docs/v1.13/consuming-messages/creating-consumer)
    - [ Subscribing to kafka topics ](/docs/v1.13/consuming-messages/subscribing-to-kafka-topics)
    - [ Consumer groups ](/docs/v1.13/consuming-messages/consumer-groups)
    - [ Message handlers ](/docs/v1.13/consuming-messages/message-handlers)
    - [ Configuring consumer options ](/docs/v1.13/consuming-messages/configuring-consumer-options)
    - [ Custom deserializers ](/docs/v1.13/consuming-messages/custom-deserializers)
    - [ Consuming messages ](/docs/v1.13/consuming-messages/consuming-messages)
    - [ Handling message batch ](/docs/v1.13/consuming-messages/handling-message-batch)
    - [ Class structure ](/docs/v1.13/consuming-messages/class-structure)
- Advanced usage
    --------------

    - [ Replacing the default serializer/deserializer ](/docs/v1.13/advanced-usage/replacing-default-serializer)
    - [ Graceful shutdown ](/docs/v1.13/advanced-usage/graceful-shutdown)
    - [ SASL Authentication ](/docs/v1.13/advanced-usage/sasl-authentication)
    - [ Custom Committers ](/docs/v1.13/advanced-usage/custom-committers)
    - [ Middlewares ](/docs/v1.13/advanced-usage/middlewares)
    - [ Stop consumer after last messages ](/docs/v1.13/advanced-usage/stop-consumer-after-last-message)
    - [ Writing custom loggers ](/docs/v1.13/advanced-usage/custom-loggers)
    - [ Setting global configurations ](/docs/v1.13/advanced-usage/setting-global-configuration)
- Testing
    -------

    - [ Kafka fake ](/docs/v1.13/testing/fake)
    - [ Assert Published ](/docs/v1.13/testing/assert-published)
    - [ Assert published On ](/docs/v1.13/testing/assert-published-on)
    - [ Assert nothing published ](/docs/v1.13/testing/assert-nothing-published)
    - [ Assert published times ](/docs/v1.13/testing/assert-published-times)
    - [ Assert published on times ](/docs/v1.13/testing/assert-published-on-times)
    - [ Mocking your kafka consumer ](/docs/v1.13/testing/mocking-your-kafka-consumer)

  Mocking your kafka consumer 
=============================

If you want to test that your consumers are working correctly, you can mock and execute the consumer to ensure that everything works as expected.

You just need to tell kafka which messages the consumer should receive and then start your consumer. This package will run all the specified messages through the consumer and stop after the last message, so you can perform whatever assertions you want to.

For example, let's say you want to test that a simple blog post was published after consuming a `post-published` message:

         ```
public function test_post_is_marked_as_published()
{
    // First, you use the fake method:
    \Junges\Kafka\Facades\Kafka::fake();

    // Then, tells Kafka what messages the consumer should receive:
    \Junges\Kafka\Facades\Kafka::shouldReceiveMessages([
        new ConsumedMessage(
            topicName: 'mark-post-as-published-topic',
            partition: 0,
            headers: [],
            body: ['post_id' => 1],
            key: null,
            offset: 0,
            timestamp: 0
        ),
    ]);

    // Now, instantiate your consumer and start consuming messages. It will consume only the messages
    // specified in `shouldReceiveMessages` method:
    $consumer = Kafka::createConsumer(['mark-post-as-published-topic'])
        ->withHandler(function (KafkaConsumerMessage $message) use (&$posts) {
            $post = Post::find($message->getBody()['post_id']);

            $post->update(['published_at' => now()->format("Y-m-d H:i:s")]);

            return 0;

        })->build();

    $consumer->consume();

    // Now, you can test if the post published_at field is not empty, or anything else you want to test:

    $this->assertNotNull($post->refresh()->published_at);
}
```

It also works for batch messages

Previous

 [    Assert published on times ](https://laravelkafka.com/docs/v1.13/testing/assert-published-on-times) 

 Sponsors

 [ version="1.0" encoding="UTF-8"?       EasyCal ](https://easycal.app/) 

 [       Search  ⌘ K   ](https://typesense.org/)
