tju*_*ugg 4 c# unit-testing moq async-await
我一直在我的个人项目中进行测试并遇到了这个小问题。我有一个创建对象列表的测试方法,我设置了一个我在我测试的方法中使用的服务以返回模拟列表。但是,由于某种原因,设置无法正常工作并且返回 null。
下面是测试方法:
var mockList = new List<IBillItem>
{
new BillItem
{
Id = 0,
DueDate = new DateTime(),
Name = "",
IndexNumber = "",
AccountNumber = "",
Amount = decimal.One
},
new BillItem
{
Id = 0,
DueDate = new DateTime(),
Name = "",
IndexNumber = "",
AccountNumber = "",
Amount = decimal.One
}
};
_billHandlingService.Setup(x => x.GetAllBillsAsync(It.IsAny<string>())).Returns(Task.FromResult(mockList));
var listBillsVm = new ListBillsViewModel(new LoggerFactory(), _billHandlingService.Object, _settingsService.Object);
await listBillsVm.GetBillsAsync();
_billHandlingService.Verify(x => x.GetAllBillsAsync(_settingsService.Name), Times.AtMostOnce);
Assert.AreEqual(1, listBillsVm.BillsList.Count);
Run Code Online (Sandbox Code Playgroud)
这是具体类 im 测试的代码:
public async Task GetBillsAsync()
{
BillsList.Clear();
var bills = await _billHandlingService.GetAllBillsAsync(_settingsService.LoggerUser);
if (null != bills)
{
var billsByDate = bills.Where(x => x.DueDate == DateTime.Today).ToList();
foreach (var bill in billsByDate)
{
BillsList.Add(bill);
RaisePropertyChanged(nameof(BillsList));
}
}
}
Run Code Online (Sandbox Code Playgroud)
我曾尝试搜索 SO / google 以获取结果,但尚未找到任何答案。提前致谢。
编辑:代码没有注释,但我相信它足够清楚,在评论中询问是否有需要清除的东西
编辑2:
Task<List<IBillItem>>GetAllBillsAsync(string username);
Run Code Online (Sandbox Code Playgroud)
是被调用方法的接口。
您可以尝试将.Returns中的更改Setup为.ReturnsAsync(mockList)
//...other code removed for brevity
_billHandlingService
.Setup(x => x.GetAllBillsAsync(It.IsAny<string>()))
.ReturnsAsync(mockList);
//...other code removed for brevity
Run Code Online (Sandbox Code Playgroud)
更新
根据您的问题使用以下最小完整可验证示例来尝试重现您的问题。请注意,我省略了创建测试所不需要的任何类。
class ListBillsViewModel {
private IBillHandlingService _billHandlingService;
private ISettingsService _settingsService;
public ListBillsViewModel(IBillHandlingService billHandlingService, ISettingsService settingsService) {
this._billHandlingService = billHandlingService;
this._settingsService = settingsService;
BillsList = new List<IBillItem>();
}
public List<IBillItem> BillsList { get; set; }
public async Task GetBillsAsync() {
BillsList.Clear();
var bills = await _billHandlingService.GetAllBillsAsync(_settingsService.LoggerUserName);
if (null != bills) {
var billsByDate = bills.Where(x => x.DueDate == DateTime.Today).ToList();
foreach (var bill in billsByDate) {
BillsList.Add(bill);
}
}
}
}
public interface ISettingsService {
string Name { get; }
string LoggerUserName { get; set; }
}
public interface IBillHandlingService {
Task<List<IBillItem>> GetAllBillsAsync(string username);
}
public class BillItem : IBillItem {
public int Id { get; set; }
public DateTime DueDate { get; set; }
public string Name { get; set; }
public string IndexNumber { get; set; }
public string AccountNumber { get; set; }
public decimal Amount { get; set; }
}
public interface IBillItem {
int Id { get; set; }
DateTime DueDate { get; set; }
string Name { get; set; }
string IndexNumber { get; set; }
string AccountNumber { get; set; }
decimal Amount { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后基于上述类重构了以下单元测试
[TestMethod]
public async Task Moq_Setup_Should_Return_List_Of_Objects() {
var mockList = new List<IBillItem>
{
new BillItem
{
Id = 0,
DueDate = DateTime.Today,
Name = "User",
IndexNumber = "",
AccountNumber = "",
Amount = decimal.One
},
new BillItem
{
Id = 1,
DueDate = DateTime.Today.AddDays(1),
Name = "User",
IndexNumber = "",
AccountNumber = "",
Amount = decimal.One
}
};
string name = "User";
var _settingsService = new Mock<ISettingsService>();
_settingsService
.Setup(m => m.Name)
.Returns(name);
_settingsService
.Setup(m => m.LoggerUserName)
.Returns(name);
var _billHandlingService = new Mock<IBillHandlingService>();
_billHandlingService
.Setup(x => x.GetAllBillsAsync(It.IsAny<string>()))
.ReturnsAsync(mockList);
var listBillsVm = new ListBillsViewModel(_billHandlingService.Object, _settingsService.Object);
await listBillsVm.GetBillsAsync();
_billHandlingService.Verify(x => x.GetAllBillsAsync(_settingsService.Name), Times.AtMostOnce);
Assert.AreEqual(1, listBillsVm.BillsList.Count);
}
Run Code Online (Sandbox Code Playgroud)
我运行了上面的测试,它按预期通过了两个设置.Returns(Task.FromResult(mockist))和.ReturnsAsync(mockList)。
您提供的示例与您的实际情况不符,或者问题超出了您在帖子中描述的内容。