I'm trying to write a unit test for my method but failed. I want to return the list with the 2 item response of GetMyRequest
with Mock( package Moq ), then I run Task.WhenAll
with the response returned with my method. So I used the SetupSequence
method but it returned a single-item list. How can I return a multi-item list in my test.
public void MyMethod()
{
ProductService = new Mock<IProductService>();
var myResponse = myStringList.Select(async x => await _myService.GetMyRequest(x, null, null)).ToList();
//my response type -> List<Task<ResponseModel>>
var myResponses = await Task.WhenAll(myResponse);
}
I want myResponses
multi-item list with mocking but it single-item.
Unit tests
public async Task InitializeAsync(){
ServiceResponse = Fixture.Build<MyModel>().CreateMany(2).ToList();
MyService = new Mock<IProductService>();
MyService.SetupSequence(service => service.GetMyRequest(It.IsAny<string>(), null, null))
.ReturnsAsync(ServiceResponse[0])
.ReturnsAsync(ServiceResponse[1]);
}
TL; DR: You can't return multiple items for your GetMyRequest
SetupSequence
means that you want to setup your GetMyRequest
in a way that multiple calls against it will result with different responses.
ServiceResponse[0]
ServiceResponse[1]
According to my understanding the return type of GetMyRequest
is Task<ServiceResponse>
, that's why you can't define a mock to return Task<IEnumerable<ServiceResponse>>
or something similar.
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.