Verifying a specific parameter with Moq

后端 未结 5 2102
生来不讨喜
生来不讨喜 2020-12-22 19:38
public void SubmitMessagesToQueue_OneMessage_SubmitSuccessfully()
{
    var messageServiceClientMock = new Mock();
    var queueableMess         


        
5条回答
  •  不思量自难忘°
    2020-12-22 20:05

    If the verification logic is non-trivial, it will be messy to write a large lambda method (as your example shows). You could put all the test statements in a separate method, but I don't like to do this because it disrupts the flow of reading the test code.

    Another option is to use a callback on the Setup call to store the value that was passed into the mocked method, and then write standard Assert methods to validate it. For example:

    // Arrange
    MyObject saveObject;
    mock.Setup(c => c.Method(It.IsAny(), It.IsAny()))
            .Callback((i, obj) => saveObject = obj)
            .Returns("xyzzy");
    
    // Act
    // ...
    
    // Assert
    // Verify Method was called once only
    mock.Verify(c => c.Method(It.IsAny(), It.IsAny()), Times.Once());
    // Assert about saveObject
    Assert.That(saveObject.TheProperty, Is.EqualTo(2));
    

提交回复
热议问题