使用moq设置一个方法来返回一个对象列表,但结果为null
本文关键字:列表 一个对象 返回 null 结果 方法 设置 moq 一个 使用 | 更新日期: 2023-09-27 17:57:37
我一直在个人项目中进行测试,遇到了这个小问题。我有一个创建对象列表的测试方法,我设置了一个在方法中使用的服务,我测试该服务以返回模拟列表。无论如何,由于某种原因,设置不起作用,并且返回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);
这是混凝土类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));
}
}
}
我试过在SO/谷歌上搜索结果,但没有找到任何答案。提前谢谢。
编辑:代码没有评论,但我相信它足够清楚,请在评论中询问是否有什么需要清除
编辑2:
Task<List<IBillItem>>GetAllBillsAsync(string username);
是被调用方法的接口。
您可以尝试将Setup
中的.Returns
更改为.ReturnsAsync(mockList)
//...other code removed for brevity
_billHandlingService
.Setup(x => x.GetAllBillsAsync(It.IsAny<string>()))
.ReturnsAsync(mockList);
//...other code removed for brevity
更新
根据你的问题,使用了以下最小的完全可验证的例子来尝试重现你的问题。请注意,我省略了创建测试所不需要的任何类。
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; }
}
然后,基于上述类重构以下单元测试
[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);
}
我运行了上面的测试,它通过了.Returns(Task.FromResult(mockist))
和.ReturnsAsync(mockList)
这两个设置的预期。
要么你给出的例子与你的实际情况不符,要么问题超出了你在帖子中描述的范围。
您需要在Task.FromResult
上指定List<IBillItem>
,如下所示:
_billHandlingService.Setup<Task<List<IBillItem>>>(
x => x.GetAllBillsAsync(It.IsAny<string>()))
.Returns(Task.FromResult<List<IBillItem>>(mockList));
此处类似SO Q和A。