简体   繁体   English

C#单元测试复杂处理程序网络

[英]C# Unit Test Complex Handler Network

I have a reasonably complex program architecture with minimal logic but lots of event handler passing. 我有一个相当复杂的程序架构,只有很少的逻辑但很多事件处理程序传递。 I'm making a skeleton of a server framework, and I have the following classes: 我正在构建一个服务器框架的框架,我有以下类:

Core , which listens to: Core ,听取:

CommHandler , which listens to: CommHandler ,听取:

CommLayer , which listens to: CommLayer ,听取:

IServerMock , which I stub out with a mocking framework (MOQ). IServerMock ,我用一个IServerMock框架(MOQ)存根。

What's the best way to unit test relationships like this that are triggered by events? 这种由事件触发的单元测试关系的最佳方法是什么? I know unit tests are supposed to be very isolated and granular things, but the only way I can think of testing this is by testing the entire process and checking the final output from Core. 我知道单元测试应该是非常孤立和细化的东西,但我能想到测试的唯一方法是测试整个过程并检查Core的最终输出。

You need to make an abstraction for each class so Core depends on an ICommHandler not CommHandler and so-on. 您需要为每个类创建一个抽象,因此Core依赖于ICommHandler而不是CommHandler等等。 The dependencies should be supplied via the constructor and that way you can easily test each class in isolation as you can mock it's dependancies. 依赖项应该通过构造函数提供,这样您就可以轻松地单独测试每个类,因为您可以模拟它的依赖性。

public class Core
{
    private ICommHandler commHandler;

    public Core(ICommHandler commHandler)
    {
        this.commHandler = commHandler;
    }
}

In your production code you can then do this: 在您的生产代码中,您可以执行以下操作:

var core = new Core(new CommHandler());

Although it would be better to use an IOC container to manage the instantiation of the objects since CommHandler has it's own dependencies but you should look into that separately. 虽然最好使用IOC容器来管理对象的实例化,因为CommHandler有它自己的依赖项,但你应该分别研究它。

Then in your Unit Test, you can do this: 然后在你的单元测试中,你可以这样做:

[Test]
public void TestSomeCommHandlerEvent()
{
     var mockCommHandler = new Mock<ICommHandler>();
     // set up the mock

     var core = new Core(mockCommHandler.Object);
}

Doing this allows you to replace the implementation of ICommHandler in your test with a mock which you can configure the behaviour of. 这样做允许您使用可以配置其行为的模拟替换测试中的ICommHandler实现。

Sounds like you can just mock the events and check that the listener performs the correct action (ie, invokes its own event). 听起来你可以只是模拟事件并检查监听器是否执行了正确的操作(即调用自己的事件)。 For example: 例如:

// Create objects
var commLayerMock = new Mock<ICommLayer>();
var commHandler = new CommHandler(commLayerMock.Object);

// subscribe to CommHandler event
bool raised = false;
commHandler.OnCommHandlerEvent += delegate { raised = true; };

// Raise CommLayer event, ensure CommHandler event was subsequently raised
commLayerMock.Raise(m => m.OnCommLayerEvent += null, new CommLayerEventArgs());
Assert.True(raised);

You'll probably want to do something like this at every level, so that you know that if the interactions between each level work properly, the entire chain will work properly. 您可能希望在每个级别执行此类操作,以便您知道如果每个级别之间的交互正常工作,整个链将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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