2

假设我有以下类(删除异常处理等):

[ServiceBehavior(InstanceContextMode = ..., ConcurrencyMode = ..., etc)]
public class MyService : IMyService  
{
    ServiceReferenceMethodsHere....
}

public class HostStartup : IHostStartup
{
    private ServiceHost _myHost;

    public ServiceHost myHost
    {
       get { return _myHost; }
       set { _myHost= value; }
    }

    public void StartService(IMyService instance)
    {
        _myHost = new ServiceHost(instance);
    }

    public void StopService()
    }
        _myHost.Close();
    {
}

现在,订阅ServiceHost.Faulted事件:

// Note: Interfaces are registered via unity container and dependency injection.
// Unity bootstrapper class not shown. 
public class TestHost : ITestHost
{
    private IHostStartup _hostStartup;
    private IMyService _myService

    public TestHost(IHostStartup hostStartup, IMyService myService)
    {
        _hostStartup = hostStartup;
        _myService = myService;
        hostStartup.StartService(_myService);
        hostStartup.myHost.Faulted += HandleHostFault;
    }

    private void HandleHostFault(object sender, EventArgs e)
    {
        HandleFaultHere.....
    }
}

好的,太好了,现在我有了处理ServiceHost.Faulted事件的代码。(当然,我还没有展示真正使我的服务工作的所有其他 WCF 胆量......)

我的问题是,如何对我的私有 HandleHostFault 方法进行单元测试而不作弊并将其设置为公共(有时我们都想这样做)。
换句话说,是否可以ServiceHost.Faulted在单元测试环境中引发事件?

我更喜欢使用 NSubstitute 进行单元测试。我尝试了以下方法:

private IHostStartup _hostStartup;
private IMyService _myService;
private TestHost _testHost;

public void MyTestInitialize()
{
    _hostStartup = Substitute.For<IHostStartup>();
    _myService = Substitute.For<IMyService>();
    _hostStartup.When(x => x.StartService(Arg.Any<IMyService>())).Do(x =>
                 {
                    _hostStartup.myHost = Substitute.For<ServiceHost>();
                 });
    _testHost  = new TestHost(_hostStartup, _myService);
}

public void HandleHostFault_InitializeHost_ServiceHostStatusOpen()
{
     // Arrange

     // Act
     hostStartup.myHost.Faulted += Raise.Event();


     // Assert
}

hostStartup.myHost.Faulted += Raise.Event()不会引发故障事件。

任何想法如何引发此 servicehost.faulted 事件?可能吗?

** 更新 **

我决定不直接测试 ServiceHost 通信对象。我认为它“无法使用 NSubstitute 进行测试”。我现在处理来自我的 HostStartup 类的 ServiceHost.Faulted 事件。

public class HostStartup : IHostStartup
{
    public event EventHandler<EventArgs> OnHostFault;

    public ServiceHost MyHost { get; private set; }

    public void StartService(IMyService instance)
    {
        MyHost = new ServiceHost(instance);
        MyHost.Faulted += HandleFault;
    }

    public void StopService()
    }
        _myHost.Close();
    {

    private void InvokeOnServiceHostFault(EventArgs eventArgs)
    {
        EventHandler<EventArgs> handler = OnHostFault;
        if (handler != null) handler(this, eventArgs);
    }

    private void HandleFault(object sender, EventArgs eventArgs)
    {
        InvokeOnServiceHostFault(eventArgs);
    }
}

// Note: Interfaces are registered via unity container and dependency injection.
// Unity bootstrapper class not shown. 
public class TestHost : ITestHost
{
    private IHostStartup _hostStartup;
    private IMyService _myService

    public TestHost(IHostStartup hostStartup, IMyService myService)
    {
        _hostStartup = hostStartup;
        _myService = myService;
        hostStartup.StartService(_myService);
        hostStartup.OnHostFault += HandleHostFault;
    }

    private void HandleHostFault(object sender, EventArgs e)
    {
        HandleFaultHere.....
    }
}

最后,单元测试操作效果很好:

public void HandleHostFault_InitializeHost_ServiceHostStatusOpen()
{
     // Arrange

     // Act
     hostStartup.OnHostFault += Raise.Event();  // works great!! :)


     // Assert
}
4

1 回答 1

0

您可以使用 PrivateObject 类对类的任何私有成员进行单元测试。有关说明和示例,请参见下面的链接

http://msdn.microsoft.com/en-us/library/bb546207.aspx

于 2012-10-05T01:31:28.850 回答