2015-10-20 41 views
1

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

可能有人請解釋一下我應該如何創建讀數的列表,然後嘲笑類&方法。或者如果這完全是錯誤的方式去解決它,請賜教?

+2

您不要模擬正在測試的類。你嘲笑它的依賴關係。 –

回答

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; 
+0

謝謝你的協助!在我放棄堅持並來到這裏之前,我有類似的東西,但是我懷疑我是否會得出這個答案。 – user2983192

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 
+0

謝謝大衛。在你和@SamHolder之間,我設法欺騙了我的勝利之路。我一定會更多地閱讀和嘗試Moq,所以我不覺得這樣的欺詐行爲。 – user2983192