使用最小起订量嵌套类和单元测试接口C# [英] Unit Test using MOQ Nested Class & Interface C#

查看:76
本文介绍了使用最小起订量嵌套类和单元测试接口C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下接口及其实现.现在,我想对Send()方法进行单元测试,该方法实际上会将消息推送到队列中. 由于我是MoQ的新手,所以不确定如何完成它.

Hi I am having the below interfaces and their implementation. Now, I wanted to unit test the Send() method which will actually push the message into a queue. Since I am new to MoQ, not sure how to get it done.

public interface IAdapter 
{
    IChannel UseQueue(QueueDetail queueDetail);
}
public interface IChannel
{
    void Send(string key, byte[] message);
}

public class AdapternServiceBus : IAdapter
{   
    readonly IConnection connection;
    readonly IModel channel;

    public AdapternServiceBus(IConnection connection, IModel channel)
    {
        this.connection = connection;     
        this.channel = channel;
    }

    public IChannel BindAndUseQueue(QueueDetail queueDetail)
    {
        // Logic of creating and binding queue
        return new ServiceBusChannel(this, queueDetail.QueueName);
    }

    public IModel GetChannel()
    {
        return channel;
    }
}

public class ServiceBusChannel : IChannel
{
    readonly string containerName;
    IModel channel;

    public ServiceBusChannel(AdapternServiceBus adapter, string containerName)
    {   
        this.containerName = containerName;
        channel = adapter.GetChannel();
    }

    public void Send(string key, byte[] message)
    {
        // Publish the message
        channel.BasicPublish(exchangeName, key, null, message);
    }
}

在工厂中,我们确定需要连接的框架类型,在工厂中,我们打开一个连接,该连接传递给下一个类,以便可以使用它创建和绑定队列. IChannel的实现是主要类,将用于与队列和主题进行实际交谈.

Here through factory, we decide which type of framework we need to connect and within the factory, we open a connection which passed on to the next class, so that it can be consumed to create and bind queues. Implementation of IChannel is the main class which will be used to actually talk with the queue and topics.

推荐答案

以下最小示例展示了如何通过提供必要的依赖项来对ServiceBusChannel.Send方法进行单元测试,以允许完成测试.它基于原始问题提供的示例.

The following minimal example shows how to unit test the ServiceBusChannel.Send method by providing the necessary dependencies to allow the test to be exercised to completion. It is based on the example provided by the original question.

[TestClass]
public class ServiceBusChannel_Should {
    [TestMethod]
    public void SendMessage() {
        //Arrange
        var channel = Mock.Of<IModel>();
        var connection = Mock.Of<IConnection>();
        var adapter = new AdapternServiceBus(connection, channel);
        var key = "somekey";
        var message = Encoding.UTF8.GetBytes("Hello World");
        var subject = new ServiceBusChannel(adapter, "containerName");

        //Act
        subject.Send(key, message);

        //Assert
        Mock.Get(channel).Verify(_ => _.BasicPublish(It.IsAny<string>(), 
                                         It.IsAny<string>(), 
                                         It.IsAny<bool>(), 
                                         It.IsAny<IBasicProperties>(), 
                                         It.IsAny<byte[]>())
                                  , Times.AtLeastOnce());
    }
}

注意: Moq 框架用于模拟抽象依赖关系.他们的快速入门提供了有关如何使用框架的示例.

Note: Moq framework was used to mock the abstract dependencies. Their Quickstart provided examples of how to use the framework.

尽管我认为被测对象不应该与具体事物联系在一起,但AdapternServiceBus可以用作模拟依赖关系的入口点,如上所述.您可以修改以满足您的特定需求,因为我假设所提供的代码不是实际使用的代码,而是示例.

While I believe that the subject under test should not be coupled to concretions, the AdapternServiceBus can be used as the entry point for the mocked dependencies as demonstrated above. You can modify to suit you specific needs as I assume the provided code is not the actual code used but an example.

这篇关于使用最小起订量嵌套类和单元测试接口C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆