测试方法时设置接口的值 [英] set value of interface in testing a method

查看:134
本文介绍了测试方法时设置接口的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在为几周前实施的课程创建单元测试。我将首先向您展示我正在进行的课程的特定部分。

so I'm currently creating a unit test for a class that I've implemented a few weeks ago. I'll show you first the particular part of the class where I'm working on.

public void PostEvent(eVtCompId inSenderComponentId, eVtEvtId inEventId, long inEventReference, IF_SerializableData inEventData)
        {
            if(mEventMap.ContainsKey(inEventId))
            {
                mEventMap[inEventId](inSenderComponentId, inEventReference, inEventData);
            }
        }

对于这种方法,我有4个参数。 1,enum; 2,另一个枚举; 3,很久; 4,接口。

For this method, I have 4 parameters. 1st, an enum; 2nd, another enum; 3rd, long; 4th, an interface.

假设我已声明/编码了此方法所需的所有正确的枚举和接口。这里的下一位是单元测试代码的一部分。

Assume that I have declared/coded all the proper enums and interface required for this method to work. This next bit right here is part of the unit test code.

target.PostEvent(eVtCompId.MainWindowsCommDevice, eVtEvtId.OnLanguageChange, 3, );

你可以看到,我还没有任何关于最后一个论点,因为我不知道我知道我应该为界面设置什么值。有任何想法吗?如果您认为需要更多信息,请随时提问。我很乐意尽力清理。

as you can see, I don't have anything yet for the last argument, because I don't know what value I should set for the interface. Any ideas? Please feel free to ask questions if you think more information is required, I'd be glad to do my best to clear things up.

推荐答案

使用模拟框架(RhinoMock, Moq ,...)并模拟界面。以下Moq示例:

Use mocking framework (RhinoMock, Moq,...) and mock the interface. Moq sample below:

var serializable = new Mock<IF_SerializableData>();
target.PostEvent(..., serializable.Object);

或者您可以在测试中手动实现接口,即本地类。

Or you can manually implement interface i.e. on local class in the test.

class MySerializable : IF_SerializableData {...}

target.PostEvent(..., new MySerializable());

这篇关于测试方法时设置接口的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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