简体   繁体   中英

unit test rabbitMQ c# Moq

I´m trying to unit test my RabbitMQ exchanges and queues, using the Moq nuget package. but no matter how I try and how much I use google, I cannot understand how I am supposed to mock rabbitMQ objects. any pointers to someone very new to unit test, mockups???

I have tried using the nuget package RabbitMQ.Fakes like so:

[TestMethod]
public void testconnectionnotnull()
{
    FakeConnectionFactory factory = new FakeConnectionFactory();
    IConnection connection = factory.CreateConnection();

    NUnit.Framework.Assert.That(connection.IsOpen);
    NUnit.Framework.Assert.That(factory.Connection, 
    NUnit.Framework.Assert.AreSame(connection));
}

but when I create the IConnection , I get following error:

System.TypeLoadException: 'Method 'CreateBasicPublishBatch' of type 'RabbitMQ.Fakes.FakeModel' from assembly 'RabbitMQ.Fakes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not implemented.'

I´ve also tried just to instantiate a standard mock object, but I cannot find the right way to do so and be able to use it.

[TestMethod]
public void TestFactoryNotNull()
{
    Mock<Rabbit_Recieve> mock = new Mock<Rabbit_Recieve>(); 
}

The easiest way to test RabbitMQ code is to write isolated unit tests for your consumer .

That is, your consumer should essentially be just a function which receives a message. So your unit tests should just create a mock rabbit message and then call the consumer with that message and then you check that the consumer does what you expect it to. In other words, the consumer should be decoupled from the Rabbit code.

You shouldn't really have to test the rabbit connection etc because that is part of the rabbit library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM