Verifying a specific parameter with Moq

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


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

    I've been verifying calls in the same manner - I believe it is the right way to do it.

    mockSomething.Verify(ms => ms.Method(
        It.IsAny(), 
        It.Is(mo => mo.Id == 5 && mo.description == "test")
      ), Times.Once());
    

    If your lambda expression becomes unwieldy, you could create a function that takes MyObject as input and outputs true/false...

    mockSomething.Verify(ms => ms.Method(
        It.IsAny(), 
        It.Is(mo => MyObjectFunc(mo))
      ), Times.Once());
    
    private bool MyObjectFunc(MyObject myObject)
    {
      return myObject.Id == 5 && myObject.description == "test";
    }
    

    Also, be aware of a bug with Mock where the error message states that the method was called multiple times when it wasn't called at all. They might have fixed it by now - but if you see that message you might consider verifying that the method was actually called.

    EDIT: Here is an example of calling verify multiple times for those scenarios where you want to verify that you call a function for each object in a list (for example).

    foreach (var item in myList)
      mockRepository.Verify(mr => mr.Update(
        It.Is(i => i.Id == item.Id && i.LastUpdated == item.LastUpdated),
        Times.Once());
    

    Same approach for setup...

    foreach (var item in myList) {
      var stuff = ... // some result specific to the item
      this.mockRepository
        .Setup(mr => mr.GetStuff(item.itemId))
        .Returns(stuff);
    }
    

    So each time GetStuff is called for that itemId, it will return stuff specific to that item. Alternatively, you could use a function that takes itemId as input and returns stuff.

    this.mockRepository
        .Setup(mr => mr.GetStuff(It.IsAny()))
        .Returns((int id) => SomeFunctionThatReturnsStuff(id));
    

    One other method I saw on a blog some time back (Phil Haack perhaps?) had setup returning from some kind of dequeue object - each time the function was called it would pull an item from a queue.

提交回复
热议问题