2014-03-26 53 views
0

我实现了一个API来处理EventLogRecord对象。为单元测试创​​建EventLogRecord

using System.Diagnostics.Eventing.Reader; 

... 
MyLog ProcessEvent(EventLogRecord eventRecord) 

现在,我想为此创建单元测试,并且EventLogRecord没有公共构造函数。如何获取EventLogRecord对象而无需连接到事件日志记录系统?

回答

0

制作像这样(非虚拟)工作的不可测项目的标准方法是使用包装。这里是思想的一些pseducode:

WrapperEventLogRecord : IEventLogRecordWrapper 
{ 
    ctor(EventLogRecord eventLogRecord) 
    public ActivityId {get{return _eventLogRecord.ActivityId;}} 
} 

现在你可以通过在界面上,并根据需要

+0

大嘲笑吧!包装将解决问题。任何其他选项?我有点担心开销(更多的代码只是为了包装)。 – Icerman

+0

如果你正在使用一个基本的模拟框架,那么没有。看到我这里没有答案的问题:http://stackoverflow.com/questions/17203061/is-a-wrapper-the-only-way-to-test-a-static-dependency你可以使用像TypeMock这样强大的模拟器来获取私人的ctor,但? –