gpt4 book ai didi

c# - 使用最小起订量设置方法以返回对象列表但得到空值

转载 作者:行者123 更新时间:2023-11-30 21:47:23 25 4
gpt4 key购买 nike

我一直在我的个人项目中进行测试并遇到了这个小问题。我有一个创建对象列表的测试方法,我设置了一个服务,我在我的方法中使用我测试返回模拟列表。然而,由于某种原因,设置无法正常工作,它返回 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);

下面是具体类即时通讯测试的代码:

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/google 中搜索结果,但仍未找到任何答案。提前致谢。

编辑:代码没有注释,但我相信它足够清楚,在注释中询问是否有需要清理的地方

编辑 2:

Task<List<IBillItem>>GetAllBillsAsync(string username); 

被调用方法的接口(interface)。

最佳答案

您可以尝试将 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) 两种设置。

您提供的示例与您的实际情况不符,或者问题超出了您在帖子中描述的范围。

关于c# - 使用最小起订量设置方法以返回对象列表但得到空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38465048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com