i'm using Moq to mock repository with async method. This method must be called 2 times. In first call of this method i need to get null value. In second i need get some parametr. If this method wasn't async then i can use
autoMockContext
.Mock<IPopulationReadRepository>()
.SetupSequence(method => method.GetCityForNewClients(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns(null)
.Returns(new Population { Id = 100, CityLongName = "Kharkiv, Kharkivska, Slobozhanshina" });
So error in last line. The result must be like this:
autoMockContext
.Mock<IPopulationReadRepository>()
.Setup(method => method.GetCityForNewClients(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns(null);
autoMockContext
.Mock<IPopulationReadRepository>()
.Setup(method => method.GetCityForNewClients(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(new Entities.Zonning.Population { Id = 100, CityLongName = "Kharkiv, Kharkivska, Slobozhanshina" });
But i need it in one invokation?
Thank you, i have resolved this problem
autoMockContext
.Mock<IPopulationReadRepository>()
.SetupSequence(method => method.GetCityForNewClients(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns(Task.FromResult<Entities.Zonning.Population>(null))
.Returns(Task.FromResult(new Entities.Zonning.Population { Id = 100, CityLongName = "Kharkiv, Kharkivska, Slobozhanshina" }));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With