In some cases when you are writing Unit Tests with Moq, you may have to return a null value from your Mocked object.
Unfortunately, if you directly attempt to return null from a Mocked object though, you will receive an error message.
Therefore, how do you return null from a Mocked object?
Well, the answer is deceptively simple! You simply assign a null value to the expected object and simply return that null object as follows:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mock<IRepository> repo = new Mock<IRepository>(); | |
MyClass returnValue = null; | |
repo.Setup(r => r.GetById(It.IsAny<string>())).Returns(returnValue); | |
No comments:
Post a Comment