Mock an update method returning a void with Moq

前端 未结 2 857
自闭症患者
自闭症患者 2021-02-01 00:14

In my test, I defined as data a List with some record in.

I\'d like setup a moq the methode Update, this method receive the user <

相关标签:
2条回答
  • 2021-02-01 00:56
    Mock<IUsers> _mockUserRepository = new Mock<IUsers>();
            _mockUserRepository.Setup(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>()))
                                .Callback((int id, string name) =>
                                    {
                                        //Your callback method here
                                    });
     //check to see how many times the method was called
     _mockUserRepository.Verify(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>()), Times.Once());
    
    0 讨论(0)
  • 2021-02-01 01:19

    If you just want to verify this method is called, you should use Verifiable() method.

    _mockUserRepository.Setup(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>()))
                       .Verifiable();
    

    If you also want to do something with those parameters, use Callback() first.

    _mockUserRepository.Setup(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>()))
                       .Callback((int id, string lastName) => {
                           //do something
                           }).Verifiable();
    

    Update

    Here's how you should mock it if you return a bool value as result.

    _mockUserRepository.Setup(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>()))
                       .Returns(true);
    
    0 讨论(0)
提交回复
热议问题