简体   繁体   English

Moq.MockException:对模拟的预期调用恰好 1 次,但为 0 次:x => x.Init()

[英]Moq.MockException: Expected invocation on the mock exactly 1 times, but was 0 times: x => x.Init()

I have this method which verify if a method is called.我有这个方法来验证是否调用了一个方法。 I am using xUnit and MOQ in C#.我在 C# 中使用 xUnit 和 MOQ。

    [Fact]
    public void VerifyIfInitCalled()
    {
        // Arrange
        var mock = new Mock<ICal>();

        var cal = new Cal(mock.Object);

        // Act
        cal.Init();

        // Assert
        mock.Verify(x => x.Init(), Times.Exactly(1));
    }

and for my Cal class对于我的 Cal class

public class Cal : ICal
{
    private ICal _cal;

    public Cal(ICal cal)
    {
        _cal = cal;
    }

    public void Init()
    {
        Console.WriteLine("Init called"); ;
    }
}

But, I run the unit test, it fails with error Moq.MockException: Expected invocation on the mock exactly 1 times, but was 0 times: x => x.Init() although I have called the Init() method.但是,我运行单元测试,它失败并出现错误Moq.MockException: Expected invocation on the mock exactly 1 times, but was 0 times: x => x.Init()尽管我调用了Init()方法。

Your need to modify your Init() to get your assert right您需要修改您的Init()以使您的断言正确

public void Init()
{
    _cal.Init();
    Console.WriteLine("Init called"); ;
}

and your interface ICal need to have an Init() member.并且您的接口ICal需要有一个Init()成员。

But clearly you have a conception problem you are implementing ICal and passing it into the class constructor.!.但显然你有一个概念问题,你正在实现ICal并将其传递给 class 构造函数。!

UPDATE更新

A unit test is specific to an implementation so your test method need to test the Cal class.单元测试特定于实现,因此您的测试方法需要测试Cal class。

If your class call an other service and you need to mock and setup a method call to get specific result you will use moq如果您的 class 调用其他服务并且您需要模拟并设置方法调用以获得特定结果,您将使用moq

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

相关问题 Moq.MockException:调用失败,模拟行为严格 - Moq.MockException: Invocation failed with mock behavior Strict 对模拟的预期调用恰好 1 次,但为 0 次 - Expected invocation on the mock exactly 1 times, but was 0 times 预期在模拟上调用一次,但使用 Moq 为 0 次 - Expected invocation on the mock once, but was 0 times With Moq Moq.MockException:模拟的以下设置不匹配 - Moq.MockException: The following setups on mock were not matched 预期对模拟的调用恰好 5 次,但正确模拟为 0 次 arguments - Expected invocation on the mock exactly 5 times, but was 0 times with properly mocked arguments MOQ 单元测试错误。 预期在模拟上调用一次,但为 0 次 - MOQ Unit testing error. Expected invocation on the mock once, but was 0 times 最小起订量与方法不匹配。 Moq.MockException:模拟上的所有调用都必须具有相应的设置 - Moq doesn't match methods. Moq.MockException: All invocations on the mock must have a corresponding setup 为什么 XUnit 测试用例会抛出“Moq.MockException:调用失败”错误,而我的输入都是正确的? - Why XUnit Test case throwing "Moq.MockException : invocation failed" error, where my inputs all are correct? 预期对该模拟调用一次,但为0次 - Expected invocation on the mock once, but was 0 times 预期在 Mock 上调用一次,但为 0 次 - Expected invocation on Mock once, but was 0 times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM