【问题标题】:Expected invocation on the mock at least once, but was never performed when trying to mock and verify a simple test预期至少在模拟上调用一次,但在尝试模拟和验证简单测试时从未执行过
【发布时间】:2019-07-30 15:04:59
【问题描述】:

我正在尝试执行一个简单的模拟和验证

[TestClass]
    public class PublisherTests
    {
        Mock<IPublisher> myPublisherMock;
        Publisher publisher;

        [TestInitialize]
        public void Initialize()
        {


            myPublisherMock = new Mock<IPublisher>();

            publisher = new Publisher(myPublisherMock.Object);

        }
        [TestMethod]
        public void ShouldReturnNegativeWhenMsgIsInvalid1()
        {
            string msg = "abc";
            long result = -1;
            myPublisherMock.Setup(m => m.GetMessageCount(msg)).Returns(result).Verifiable();
            publisher.GetMessageCount(msg);
            myPublisherMock.Verify(m => m.GetMessageCount(msg));

        }

    }

但它总是抛出异常

Moq.MockException: ' 预期在模拟上至少调用一次, 但从未执行过:m => m.GetQueueMessageCount1(It.IsAny())

Performed invocations:

   Mock<IPublisher:1> (m):
   No invocations performed.

实际方法

public class Publisher : IPublisher
{   
 private IPublisher MyPublisher { get; set; }
    public Publisher(IPublisher publisher)
    {
        MyPublisher = publisher;
    }

    public long GetMessageCount(string msg)
   {
    long result = 0;

    try
    {
        if (msg == "abc")
            throw new Exception();

        return result;
    }
    catch (Exception ex)
    {
        var p = ex.Message;
        return result = -1;
    }


   }
}

无法确定我哪里出错了。

【问题讨论】:

  • @Nkosi 我确实调用了模拟,但仍然抛出错误。
  • 你在哪里调用模拟?
  • @LasseVågsætherKarlsen 请查看最新编辑 .publisher.GetMessageCount(msg); - 我相信这是你提到的那个吧?
  • 不,它在 Publisher 对象上调用类似的方法,而不是模拟。您确实将模拟 传递给 该发布者对象,但我们看不到发布者的 GetMessageCount 中的内容。
  • @Nkosi 我已经添加了 Publisher 类的详细信息。

标签: c# unit-testing nunit moq


【解决方案1】:

基于所示示例并假设这是对装饰器模式的测试

例如

public interface IPublisher {
    long GetMessageCount(string msg);
}

public class Publisher : IPublisher {
    private readonly IPublisher publisher;

    public Publisher(IPublisher publisher) {
        this.publisher = publisher;
    }

    public long GetMessageCount(string msg) {
        long result = 0;
        try {
            if (msg == "abc")
                throw new Exception();

            result = publisher.GetMessageCount(msg);
            return result;
        } catch (Exception ex) {
            var p = ex.Message;
            return result = -1;
        }
    }
}

如果测试想要测试抛出异常时的预期行为,则无需在模拟上设置该成员,因为它预计不会被调用。

但是,您可以验证它从未被调用过。

例如

[TestMethod]
public void ShouldReturnNegativeWhenMsgIsAbc() {
    //Arrange
    var myPublisherMock = new Mock<IPublisher>();

    long expected = -1;

    var subject = new Publisher(myPublisherMock.Object);

    //Act
    var actual = subject.GetMessageCount("abc");

    //Assert
    actual.Should().Be(expected); //FluentAssertion
    myPublisherMock.Verify(_ => _.GetMessageCount(It.IsAny<string>()), Times.Never);
}

【讨论】:

    猜你喜欢
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多