我想为下面提到的PublishDataProvider创建单元测试。由于我是新来的单元测试。我尝试了以下方法,但即时通讯出错。我迷恋我尝试的方式是错误的。谁能以正确的方式指导我。
类文件代码:PublishDataProvider.cs
private DataRouter _dataRouter;
public event EventHandler<PhysiologyData> DataUpdated;
public PublishDataProvider(IUnityContainer container)
{
_DataRouter = container.Resolve<DataRouter>(new ResolverOverride[] {
new ParameterOverride("clienttype",ClientType.VCG)});
if(_physiologyDataRouter != null)
{
_dataRouter.ModuleDataReceived += OnModuleDataReceived;
}
}
private void OnModuleDataReceived(ModuleData moduledata)
{
moduledata.DataType = DataType.ModuleData;
DataUpdated?.Invoke(this, moduledata);
}
文件DataRouter.cs
public event ModuleDataEventHandler ModuleDataReceived;
private void SendBatteryStatus(object sender, EventArgs e)
{
ModuleDataReceived?.Invoke(physmoduleinfo);
}
测试文件:Test.cs
protected static IUnityContainer Container { get; set; }
protected IDataProvider DataProvider { get; set; }
private DataRouter _dataRouter;
[Test]
public void TestPublishDataProvider()
{
Container = new UnityContainer();
Container.LoadConfiguration();
_dataRouter = MockRepository.GenerateMock<DataRouter>(ClientType.VCG, Container);
Container.RegisterInstance(_dataRouter, new ContainerControlledLifetimeManager());
MarkerDataBuffer MarkerDataBuffer = new MarkerDataBuffer();
_physiologyDataRouter.Raise(x => x.MarkerDataReceived += null, MarkerDataBuffer);
var wasCalled = false;
_physiologyDataRouter.MarkerDataReceived += (e) => wasCalled = true;
NUnit.Framework.Assert.IsTrue(wasCalled);
错误:
System.InvalidOperationException:无效的调用,上次调用已使用或未进行任何调用(请确保您正在调用虚拟(C#)/可重写(VB)方法)。