Using moq to mock only some methods

前端 未结 3 984
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 07:00

I have the following method:

public CustomObect MyMethod()
{
    var lUser = GetCurrentUser();
    if (lUser.HaveAccess)
    {
        //One behavior
    }
          


        
3条回答
  •  广开言路
    2020-12-01 07:45

    This is called a partial mock and the way I know to do it in moq requires mocking the class rather than the interface and then setting the "Callbase" property on your mocked object to "true".

    This will require making all the methods and properties of the class you are testing virtual. Assuming this isn't a problem, you can then write a test like this:

     var mock = new Mock();
     mock.CallBase = true;
     mock.Setup(x => x.GetCurrentUser()).Returns(lUnauthorizedUser);
     mockedTest.Object.MyMethod();
    

提交回复
热议问题