【问题标题】:Create EventLogRecord for unit test为单元测试创​​建 EventLogRecord
【发布时间】:2015-02-06 06:21:53
【问题描述】:

我实现了一个 API 来处理 EventLogRecord 对象。

using System.Diagnostics.Eventing.Reader;

...
MyLog ProcessEvent(EventLogRecord eventRecord)

现在,我想为此创建单元测试,EventLogRecord 没有公共构造函数。如何在不连接事件日志系统的情况下获取EventLogRecord 对象?

【问题讨论】:

    标签: c# unit-testing event-log


    【解决方案1】:

    使此类不可测试项目(非虚拟)工作的标准方法是使用包装器。这是这个想法的一些伪代码:

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

    现在您可以传入接口并根据需要对其进行模拟

    【讨论】:

    • 太棒了!包装将解决问题。还有其他选择吗?我有点担心开销(更多代码只是为了包装)。
    • 如果您使用的是基本的模拟框架,那么不会。请在此处查看我未回答的问题:stackoverflow.com/questions/17203061/… 不过,您可以使用像 TypeMock 这样功能强大的模拟程序来获取私有 ctor?
    猜你喜欢
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多