Verifying a specific parameter with Moq

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


        
5条回答
  •  抹茶落季
    2020-12-22 20:16

    Had one of these as well, but the parameter of the action was an interface with no public properties. Ended up using It.Is() with a seperate method and within this method had to do some mocking of the interface

    public interface IQuery
    {
        IQuery SetSomeFields(string info);
    }
    
    void DoSomeQuerying(Action queryThing);
    
    mockedObject.Setup(m => m.DoSomeQuerying(It.Is>(q => MyCheckingMethod(q)));
    
    private bool MyCheckingMethod(Action queryAction)
    {
        var mockQuery = new Mock();
        mockQuery.Setup(m => m.SetSomeFields(It.Is(s => s.MeetsSomeCondition())
        queryAction.Invoke(mockQuery.Object);
        mockQuery.Verify(m => m.SetSomeFields(It.Is(s => s.MeetsSomeCondition(), Times.Once)
        return true
    }
    

提交回复
热议问题