Reputation: 643
First off, I am new at testing, so this may be a dumb question. I am currently creating unit tests for my classes. I have one property that depending on a nullable property will send off a new Comment object to the database. Usually with unit testing I would just make sure an object got sent to my Mock Service, and call it good. However, would it not make more sense to test the values on the Comment Object to make sure it is going down the correct path, instead of just assuming it did. Here is a sample of the code I am testing:
if (DeliveryDate != null)
{
AddPartHistory("Delivery Date Changed from " + ((DateTime)DeliveryDate).ToShortDateString() + " to " + ((DateTime)value).ToShortDateString());
}
else
{
AddPartHistory("Delivered Date of " + ((DateTime)value).ToShortDateString() + " was added.");
}
The AddPartHistory
function sends the Comment object (which holds the text in a Property called Entry) to the database (or to a Mock Service during test), and stores it in a Property called NewPartHistory
. And here is the code that I believe may be more of an integration test:
vm.DeliveryDate = DateTime.UtcNow;
Assert.AreEqual("Delivered Date of " + ((DateTime)vm.DeliveryDate).ToShortDateString() + " was added.", vm.NewPartHistory.Entry);
OldDeliveryDate = vm.DeliveryDate;
vm.DeliveryDate = DateTime.UtcNow;
Assert.AreEqual("Delivery Date Changed from " + ((DateTime)OldDeliveryDate).ToShortDateString() + " to " + ((DateTime)vm.DeliveryDate).ToShortDateString(), vm.NewPartHistory.Entry);
So, back to the question, should I leave this code in the unit test, or move to integration test.
Since there was a lot of talk about my AddPartHistory
Method, here it is. It simply fills out the standard data for a PartHistory
(which is always the same), adds the Entry, and then updates a Listview
with new data:
private void AddPartHistory(string historyText)
{
NewPartHistory = new CdaService.PartHistory();
NewPartHistory.EnteredBy = User.Current.UID;
NewPartHistory.Entry = historyText;
NewPartHistory.EntryDate = DateTime.UtcNow;
NewPartHistory.PartId = ThisPart.Id;
webService.Insert(NewPartHistory);
GetPartHistory();
}
Upvotes: 2
Views: 275
Reputation: 39248
I would change it to pass in the new Comment object to the AddPartHistory method - instead of passing it the values to build the object. That way you can unit test the logic including the construction of the comment object. It would also be helpful to return the comment object from AddPartHistory to make it easier to assert.
Move this out of the method like this:
NewPartHistory = new CdaService.PartHistory();
NewPartHistory.EnteredBy = User.Current.UID;
NewPartHistory.EntryDate = DateTime.UtcNow;
NewPartHistory.PartId = ThisPart.Id;
if()
{
NewPartHistory.Entry = "Delivered Date of"......;
return AddPartHistory(NewPartHistory );
}
else
{
NewPartHistory.Entry = "Delivery Date Changed from".....;
return AddPartHistory(NewPartHistory );
}
//return comment object from AddPartHistory so that you can call this entire method and assert all properties
Upvotes: 2