gpt4 book ai didi

c# - 测试采用 IQueryable 参数的公共(public)类方法

转载 作者:太空宇宙 更新时间:2023-11-03 18:26:33 25 4
gpt4 key购买 nike

我是 C# 新手,刚接触测试。我在这里和其他在线页面上阅读了几个讨论使用 Moq 进行单元测试的问题,但我就是不明白。

以下是我希望使用硬编码读数列表测试的表达式。

public class RequiredStations
{
//private readonly WundergroundEntities _database;

public List<Reading> Stations(IQueryable<Reading> readingsList)
{
return readingsList.GroupBy(r => r.WeatherStationID)
.Select(grp => new Reading { WeatherStationID = grp.Key, Date_Taken = grp.Max(dt => dt.Date_Taken) })
.ToList();

}
}

以下是我最近对上述内容进行的拙劣尝试...

[TestClass]
public class RequiredStations_Tests
{
[TestMethod]
public void TestingWeatherStationsRequiredLinq()
{
IList<Reading> readings = new List<Reading>
{
new Reading { WeatherStationID = "1", ReadingID = 1, Reading_Date = new DateTime(01/01/2014), Date_Taken = new DateTime(01/01/2014), Avg_Temperature = 6, Max_Temperature = 8, Min_Temperature = 2},
new Reading { WeatherStationID = "1", ReadingID = 2, Reading_Date = new DateTime(02/01/2014), Date_Taken = new DateTime(02/01/2014), Avg_Temperature = 1, Max_Temperature = 1, Min_Temperature = 1},
new Reading { WeatherStationID = "1", ReadingID = 3, Reading_Date = new DateTime(03/01/2014), Date_Taken = new DateTime(03/01/2014), Avg_Temperature = 3, Max_Temperature = 3, Min_Temperature = 3},
new Reading { WeatherStationID = "2", ReadingID = 4, Reading_Date = new DateTime(01/02/2014), Date_Taken = new DateTime(01/02/2014), Avg_Temperature = 8, Max_Temperature = 8, Min_Temperature = 8},
new Reading { WeatherStationID = "2", ReadingID = 5, Reading_Date = new DateTime(01/03/2014), Date_Taken = new DateTime(01/03/2014), Avg_Temperature = 9, Max_Temperature = 9, Min_Temperature = 9},
new Reading { WeatherStationID = "2", ReadingID = 6, Reading_Date = new DateTime(01/04/2014), Date_Taken = new DateTime(01/04/2014), Avg_Temperature = 11, Max_Temperature = 11, Min_Temperature = 11}

};

Mock<RequiredStations> requiredStations = new Mock<RequiredStations>();

有人可以解释一下我应该如何创建阅读列表,然后模拟类和方法。或者,如果这完全是错误的做法,请赐教?

最佳答案

您已经创建了您的 List<Readings> ,我认为您可以将它传递给要测试的方法,调用 AsQueryable()上的扩展方法。

像这样:

[TestMethod]
public void TestingWeatherStationsRequiredLinq()
{
List<Reading> readings = new List<Reading>
{
new Reading { WeatherStationID = "1", ReadingID = 1, Reading_Date = new DateTime(01/01/2014), Date_Taken = new DateTime(01/01/2014), Avg_Temperature = 6, Max_Temperature = 8, Min_Temperature = 2},
new Reading { WeatherStationID = "1", ReadingID = 2, Reading_Date = new DateTime(02/01/2014), Date_Taken = new DateTime(02/01/2014), Avg_Temperature = 1, Max_Temperature = 1, Min_Temperature = 1},
new Reading { WeatherStationID = "1", ReadingID = 3, Reading_Date = new DateTime(03/01/2014), Date_Taken = new DateTime(03/01/2014), Avg_Temperature = 3, Max_Temperature = 3, Min_Temperature = 3},
new Reading { WeatherStationID = "2", ReadingID = 4, Reading_Date = new DateTime(01/02/2014), Date_Taken = new DateTime(01/02/2014), Avg_Temperature = 8, Max_Temperature = 8, Min_Temperature = 8},
new Reading { WeatherStationID = "2", ReadingID = 5, Reading_Date = new DateTime(01/03/2014), Date_Taken = new DateTime(01/03/2014), Avg_Temperature = 9, Max_Temperature = 9, Min_Temperature = 9},
new Reading { WeatherStationID = "2", ReadingID = 6, Reading_Date = new DateTime(01/04/2014), Date_Taken = new DateTime(01/04/2014), Avg_Temperature = 11, Max_Temperature = 11, Min_Temperature = 11}
};

RequiredStations requiredStations = new RequiredStations();
var result = requiredStations.Stations(readings.AsQueryable());
//assert things about result here
}

如果您没有看到 AsQueryable() 方法,那么您可能需要将其添加到您的 using 语句中:

using System.Linq;

关于c# - 测试采用 IQueryable 参数的公共(public)类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33240529/

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