set value of interface in testing a method

守給你的承諾、 提交于 2019-12-13 03:08:20

问题


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);
            }
        }

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.


回答1:


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());


来源:https://stackoverflow.com/questions/13655557/set-value-of-interface-in-testing-a-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!