NUnit Test Coverage

I want to write a test against a property that calls a PropertyChanged event handler when no class has subscribed to the event as a test to insure that the event handler does not call a null EventHandler delagate. The test is utilizing the NUnit framework. Here is the test I have written so far:

     [Test]
     public void PropertyChangedEventDoesNotFireWithoutHandlers()
     {
          _instance.IsSelected = true;
          Assert.Pass();
     }


I figure that this test will fail, if an exception is generated if the PropertyChanged event handler tries to call a null delagate, so the Assert.Pass() statement would never be reached.

In the case of the instantiated class IsSelected is false by default, so setting it to true causes the event to fire. (I've testing that with another test method.) And dotCoverage shows that the relavant code in the event handler is covered by the test. But the test itself is only marked as 3/4 covered. The uncovered code according to highlighting is the last bracket character.

I have a couple questions about this. First why does dotCover mark the method as 3/4 covered? Second is there a better way to write the test, so that the whole test is covered?
2 comments

Hello Gil,

Unfortunately, there's a known issue with closing braces in dotCover: dotCover considers as uncovered closing braces in methods that always return with exception. NUnit's Assert.Pass() method actually always throws an exception of type SuccessException, that's why the closing brace in your test is highlighted as uncovered.

We have a corresponding issue in our tracker: http://youtrack.jetbrains.com/issue/DCVR-4846. You are welcome to track its status and comment.

As an alternative for your test I can suggest you using Assert.DoesNotThrow() method:

[Test]
public void PropertyChangedEventDoesNotFireWithoutHandlers()
{
  Assert.DoesNotThrow(() => { _instance.IsSelected = true; });
}


Or you can leave your test without any asserts (it will fail automatically in case of exception):

[Test]
public void PropertyChangedEventDoesNotFireWithoutHandlers()
{
  _instance.IsSelected = true;
}


Please do not hesitate to contact me if you have any questions. Best regards.

0

Thank you! That's just what I needed.

Gil

0

Please sign in to leave a comment.