Using Moq, how do I set up a method call with an input parameter as an object with expected property values?

前端 未结 3 2029
一生所求
一生所求 2021-02-01 14:35
 var storageManager = new Mock(); 
 storageManager.Setup(e => e.Add(It.IsAny()));

The Add() method expect

3条回答
  •  庸人自扰
    2021-02-01 15:22

    You can use Verify.

    Examples:

    Verify that Add was never called with an UserMetaData with FirstName!= "FirstName1":

    storageManager.Verify(e => e.Add(It.Is(d => d.FirstName!="FirstName1")), Times.Never());
    

    Verify that Add was called at least once with an UserMetaData with FirstName== "FirstName1":

    storageManager.Verify(e => e.Add(It.Is(d => d.FirstName=="FirstName1")), Times.AtLeastOnce());
    

    Verify that Add was called exactly once with FirstName == "Firstname1" and LastName == "LastName2":

    storageManager.Setup(e => e.Add(It.Is(data => data.FirstName == "FirstName1"
                                                             && data.LastName  == "LastName2")));
    
    ...
    
    storageManager.VerifyAll();
    

提交回复
热议问题