[英]how can i get the selected value of a multi-item checklistbox, c#, winforms
[英]How can I setup a multi-item list with moq
我正在嘗試為我的方法編寫單元測試但失敗了。 我想返回帶有 Mock( package Moq ) 的GetMyRequest
的 2 項響應的列表,然后我運行Task.WhenAll
並使用我的方法返回響應。 所以我使用了SetupSequence
方法,但它返回了一個單項列表。 如何在我的測試中返回一個多項目列表。
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);
}
我想要帶有 mocking 的myResponses
多項目列表,但它是單項目。
單元測試
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:您不能為GetMyRequest
返回多個項目
SetupSequence
意味着您希望設置GetMyRequest
的方式使得針對它的多次調用將導致不同的響應。
ServiceResponse[0]
的值ServiceResponse[1]
的值根據我的理解, GetMyRequest
的返回類型是Task<ServiceResponse>
,這就是為什么你不能定義一個模擬來返回Task<IEnumerable<ServiceResponse>>
或類似的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.