I am trying to figure how to use When method for my use.
When(Func<bool> condition);
Another post here has one example of usa of When method
var mockedService = new Mock<IFormatProvider>();
mockedService.When(() => DateTime.Now.Hour < 12).Setup(x => x.GetFormat(typeof(string))).Returns(null);
What I am trying to do is I have a variable called x. I want to use When method so it performs action only when X is not Null.
for example
mockedService.When(()=> !null x).Returns(x)
or
mockedService.When(condition => x).Returns(x);
Both of the above lines of code are not working syntex is not correct. Any idea how to write it correctly. Thanks
You must Setup method to return:
mockedService.When(() => null != x)
.Setup(s => s.GetFormat(It.IsAny<Type>()))
.Returns(x);
Another way:
mockedService
.Setup(s => s.GetFormat(It.Is<Type>(t => x != null)))
.Returns(x);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.