jeudi 28 mars 2019

What is the best practise for writing language feature unit test?

What is the best practice in writing unit test for the method when inside core .NET functions only?

I am new to unit test and I read a lot about best practices of writing unit test and they recommend not to test language features (like to ensure property returns value). My case seems to be related to native code only, but I can't 100% be sure that it is language feature and I can skip writing test. Making test for this method looks like I'm writing test for .NET library itself.

I wanted to set expected string value and compare to it, but date.ToString() is culture specific.

public static string ConvertDateToDateTimeString(this DateTime date)
{
  return new DateTimeOffset(date, TimeZoneInfo.Local.GetUtcOffset(date)).ToString("o");
}

I created simple test, but when I am trying to set expected value, I realise that it should be the same as ConvertDateToDateTimeString() implementation.

Doing this makes this unit test useless.

[TestMethod]
public void ConvertDateToDateTimeString_WhenCalled_ReturnConvertedDateTimeString()
{
  var date = new DateTime(....)

  var actual = date.ConvertDateToDateTimeString();

  string expected = ...

  Assert.AreEqual(expected, actual);
}

If I skip writing test for this than code coverage won't be full.

Could you please recommend any useful practices what to do in such cases?

Aucun commentaire:

Enregistrer un commentaire