Reputation: 13
I am new to C# and newer to testing. I have read several questions here and other pages online discussing unit testing with Moq, but I'm just not getting it.
The following is an expression I wish to test using a list of hard coded Readings.
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();
}
}
The following is my latest botched attempt at testing the above...
[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>();
Could someone please explain how I should create the list of readings, and then Mock the class & method. Or if this is completely the wrong way to go about it, please enlighten me?
Upvotes: 1
Views: 274
Reputation: 32946
you have created your List<Readings>
, I think you can just pass it to your method to be tested calling the AsQueryable()
extension method on it.
something like this:
[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
}
if you don't see the AsQueryable() method then you might need to add this to your using statements:
using System.Linq;
Upvotes: 4
Reputation: 218960
There's nothing here to be mocked. The code being tested has no dependencies. It simply accepts a collection and returns a modified collection:
// 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
Upvotes: 3