简体   繁体   English

来自 Rhino Mocks 的 ExpectationViolationException 用于 .NET 中的单元测试

[英]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我期待 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"));
        }            
    }

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

 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.在首次调用该方法期间,在 StopManager() 中的 LogWarning 中引发了期望,这对我来说没有任何意义。

What is the issue here ?这里有什么问题?

You've told Rhino to expect a call to LogWarning with four arguments, but you only call it with three.您已经告诉 Rhino 期望使用四个参数调用 LogWarning,但您只使用三个参数调用它。 It probably thinks you're calling a different overload.它可能认为您正在调用不同的重载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM