假设我有以下类(删除异常处理等):
[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
}
答案 0 :(得分:0)
您可以使用PrivateObject类对任何类的私有成员进行单元测试。 请参阅以下链接以获取解释和示例