我是C#新手,測試更新。我在這裏閱讀了幾個問題,並在網上討論了與Moq進行單元測試的其他頁面,但我只是沒有得到它。測試公共類採用IQueryable參數的方法
以下是我希望使用硬編碼讀物列表進行測試的表達式。
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>();
可能有人請解釋一下我應該如何創建讀數的列表,然後嘲笑類&方法。或者如果這完全是錯誤的方式去解決它,請賜教?
您不要模擬正在測試的類。你嘲笑它的依賴關係。 –