Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use MassTransit test harness to test Consumer with constructor dependency injection?

I have some message consumers that take dependencies through the constructor, and I would like to cover them in unit tests. Does MassTransit's test harness provide a way to register consumers with constructor parameters?

like image 552
RAM Avatar asked Mar 14 '19 18:03

RAM


1 Answers

You can specify a factory method, or a consumer factory, when creating a consumer test harness.

harness.Consumer<T>(() => new T());

You can find the three standard extension methods in the code:

https://github.com/MassTransit/MassTransit/blob/develop/src/MassTransit/Testing/ConsumerTestHarnessExtensions.cs#L35

UPDATE: MassTransit now has container support for the test harness

like image 151
Chris Patterson Avatar answered Oct 23 '22 01:10

Chris Patterson