Trying to write a unit test for a function that checks some log records and adds them to a collection of logs inside the object if they're more recent than the current sample. Here's the function.
public void RefreshQueue()
{
DateTime latest = Logs.Select(x => x.Datetime).FirstOrDefault();
if(latest == null)
{
latest = DateTime.Now;
}
var newLogs = rep.Logs_Get().Where(x => x.Severity == "Info" && x.Datetime > latest).ToList();
if (newLogs.Any())
{
Logs = Logs.Union(newLogs).OrderByDescending(x => x.LogID);
}
}
But I'm at a loss as to how to test this. In my unit test code there's a Moq mock of the repository that returns some logs. But it's passed into the constructor when the class is created. I need to verify that the number of Logs increases after RefreshQueue is called. But if I create DateTime properties on my fake logs, the code will automatically pick the most recent, and look for anything newer. Which there will never be, unless I can update the repository.
The only thing I can think of to do is this:
[TestMethod]
public void AssertRefreshQueueAddsLatestLogs()
{
Mock<IRepository> repMock = GetMockRepository();
DebugViewModel dVm = new DebugViewModel(repMock.Object);
Assert.AreEqual(dVm.Logs.Count(), 1);
repMock = GetMockRepositoryWithNewLogs();
dVm = new DebugViewModel(repMock.Object);
Assert.AreEqual(dVm.Logs.Count(), 2);
}
Which doesn't seem like a very good test at all. I understand I can use sequences or a queue to ensure that my mock returns different lists each time that Logs_Get() is called, but that doesn't seem any different to just grabbing a new list with different Logs in it.
Is my current test fair? If not, is it possible to "refresh" the mock repository inside the object? If not, how can I create a fair and meaningful test of this function?
Aucun commentaire:
Enregistrer un commentaire