测试公共类采用IQueryable参数的方法

时间:2015-10-20 15:21:25

标签: c# unit-testing moq

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

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

2 个答案:

答案 0 :(得分:4)

您已创建了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;

答案 1 :(得分:3)

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

// 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