Mock an update method returning a void with Moq

谁说胖子不能爱 提交于 2019-12-02 20:00:37

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);
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());
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!