Using Moq to Mock a Func<> constructor parameter and Verify it was called twice

前端 未结 3 587
萌比男神i
萌比男神i 2021-01-03 18:59

Taken the question from this article (How to moq a Func) and adapted it as the answer is not correct.

public class FooBar
{
    private Func

        
3条回答
  •  悲&欢浪女
    2021-01-03 19:15

    I don't think it is necessary to use a mock for the Func.

    You can simply create an ordinary Func yourself that returns a mock of IFooBarProxy:

    int numberOfCalls = 0;
    Func func = () => { ++numberOfCalls;
                                      return new Mock(); };
    
    var sut = new FooBar(func);
    
    sut.Process();
    
    Assert.Equal(2, numberOfCalls);
    

提交回复
热议问题