mardi 27 octobre 2015

ExpectationViolationException from Rhino Mocks for Unit testing in .NET

I have following Unit Test:

    [Test]
    public void ReportGeneratorCancelTwiceTest()
    {
        var logger = this.mockRepository.StrictMock<ILog>();
        this.manager = new ReportGeneratorManager(logger, new Guid(), 5, null);
        using (this.mockRepository.Record())
        {
            Expect.Call(() => this.log.LogWarning(null, null, null, null)).IgnoreArguments().Repeat.AtLeastOnce();
        }

        using (this.mockRepository.Playback())
        {
            var action = new Action(() => this.OnStart());
            action.BeginInvoke(null, null);
            Thread.Sleep(5000);
            this.OnStop();
            Thread.Sleep(5000);
            this.OnStop();
        }
    }

    private void OnStart()
    {
        this.manager.StartManager();
    }

    private void OnStop()
    {
        this.manager.StopManager();
    }
}

I'm expecting a LogWarning, which will be called in StopManager() method

  public void StopManager()
    {
        if (this.RunningTask > 0)
        {
            this.logger.LogEvent(this.Id.ToString(), string.Format(CultureInfo.InvariantCulture, "System"), string.Format(CultureInfo.InvariantCulture, "Cancelling tasks"));
            this.CancellationTokenSource.Cancel();
        }
        else
        {
            this.logger.LogWarning(this.Id.ToString(), string.Format(CultureInfo.InvariantCulture, "System"), string.Format(CultureInfo.InvariantCulture, "Tasks are already in cancelled state"));
        }            
    }

So considering the Playback() block in mockRepository, the first call on OnStop() should cancel the tasks, and then it is clear that the second call to OnStop() Logs the LogWarning, which I expected, but I'm receiving following Rhino Mocks exception:

 Rhino.Mocks.Exceptions.ExpectationViolationException : ILog.LogWarning("00000000-0000-0000-0000-000000000000", "System", "Tasks are already in cancelled state"); Expected #0, Actual #1.

The expection is thrown at the LogWarning in StopManager() during the FIRST call to the method, which does not make any sense to me.

What is the issue here ?

Aucun commentaire:

Enregistrer un commentaire