.NET中单元测试的Rhino Mocks中的ExpectionViolationException

本文关键字:Mocks 中的 ExpectionViolationException Rhino 单元测试 NET | 更新日期: 2023-09-27 18:19:27

我有以下单元测试:

    [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();
    }
}

我期待一个LogWarning,它将在StopManager()方法中调用

  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"));
        }            
    }

因此,考虑到mockRepository中的Playback()块,对OnStop()的第一次调用应该会取消任务,然后很明显,对OnStop()的第二次调用会记录LogWarning,这是我所期望的,但我收到了以下Rhino Mocks异常:

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

在对该方法的第一次调用期间,期望值被抛出到StopManager()中的LogWarning,这对我来说没有任何意义

这里的问题是什么?

.NET中单元测试的Rhino Mocks中的ExpectionViolationException

您已经告诉Rhino期望用四个参数调用LogWarning,但您只用三个参数调用它。它可能认为你在调用另一个过载。