I want to mock the DataService class for the GetData method.
Interface IProvider
{
Response GetAccountInfo();
}
public class Provider:IProvider
{
public Response GetAccountInfo()
{
Response resp = new Response();
Response resp1 = new Response();
DataService dataService = new DataService();
resp = dataService.GetData("Girish" , "Advani" );
resp1 = dataService.GetData("Pranav" , "Kawle" );
}
}
I am getting value in resp object but the resp1 object is null. I need the value of both objects. Could you please provide me solution for it?
If I understand your question properly, you need to call same method twice during on the mocked object.
You can use SetupSequence for this purpose.
e.g. Below, the test method calls the GetNextStuff method twice and returns one of the values alternatingly, each time it is called.
[Test]
public void MogMethodThatReturnsADifferentValueWhenCalledASecondTimeUsingSequences()
{
Mock<ISomeService> _mockSomeService = new Mock<ISomeService>();
_mockSomeService.SetupSequence(x => x.GetNextStuff())
.Returns(new SomeStuff {Id = 1, Name = "Real"})
.Returns((SomeStuff)null);
Assert.IsNotNull(_mockSomeService.Object.GetNextStuff());
Assert.IsNull(_mockSomeService.Object.GetNextStuff());
}
You can refer this blog for more details.
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