0

I want to create unit test for PublishDataProvider mentioned below. As i am new to Unit test. i tried the below method but im getting error. I dout the way i am trying is wrong. can anyone please guide me in right way.

Class file Code: 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);
    }

File DataRouter.cs

public event ModuleDataEventHandler ModuleDataReceived;

 private void SendBatteryStatus(object sender, EventArgs e)
    {
     ModuleDataReceived?.Invoke(physmoduleinfo);
    }

Test File: 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);

Error:

System.InvalidOperationException : Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method).

Geeth
  • 5,096
  • 20
  • 75
  • 129
  • _dataRouter.Raise(x => x.ModuleDataReceived += null, moduleData);} – Geeth Jan 13 '19 at 04:46
  • @Geeth that is.important information, which you should [edit] your question to add. While you are at it, check the code you have put there because I don't think it will compile, so I'm not even going to try to diagnose your issue. Also, try putting '> ' (greater-than, space) before your exception message. – Richardissimo Jan 13 '19 at 06:43
  • This could be related to Mock that your trying to do, You could try making DataProvider and others virtual methods. Furthermore, more info on exception and complete code snippet would make it easier to help you. – Hasitha Jan 13 '19 at 06:44
  • Following like helped me to achieve my Unit test. [Unit test for Private methods](https://stackoverflow.com/questions/9122708/unit-testing-private-methods-in-c-sharp) – Geeth Jan 15 '19 at 16:57

0 Answers0