测试一个接受IQueryable参数的公共类Method

本文关键字:参数 Method IQueryable 一个 测试 | 更新日期: 2023-09-27 18:18:28

我是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>();

谁能解释一下我应该如何创建阅读清单,然后模拟课堂& &;方法。或者如果这是完全错误的做法,请告诉我?

测试一个接受IQueryable参数的公共类Method

你已经创建了你的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;

这里没有什么可嘲笑的。被测试的代码没有依赖项。它只是接受一个集合并返回一个修改后的集合:

// arrange
IList<Reading> readings = new List<Reading>
{
    // build your hard-coded data
};
RequiredStations requiredStations = new RequiredStations();
// act
var result = requiredStations.Stations(readings);
// assert
// examine "result" to make sure it is what you expect