1

In this small piece of code:

public event StabilityChangedHandler StabilityChanged;

267   private void RaiseStabilityChanged()
      {
          if (StabilityChanged != null)
          {
  1           StabilityChanged();
          }
275   }

NCover reports only 50% branch coverage for this Method. However, I know that I RaiseStabilityChanged() is being called with and without handlers being hooked up to the StabilityChangedEvent. On the left I put the hit counts reported by NCover.

Why might NCover be reporting 50% instead of 100%?

whatsisname
  • 5,872
  • 2
  • 20
  • 27
  • 1
    As a sidenote, it's usual to implement events so that subscription/unsubscription is threadsafe. So you should copy `StabilityChanged` to a local variable, and then check and call that variable. – CodesInChaos Jan 12 '12 at 22:10

1 Answers1

0

It appears that no test is ever run where StabilityChanged == null. Probably not an important test to run, but it will get your coverage to 100%

therealmitchconnors
  • 2,732
  • 1
  • 18
  • 36
  • They said that it was run when there was no handler, which would mean it was null. – Jon Hanna Jan 12 '12 at 22:34
  • If that were true, it would be 100% code coverage... try setting conditional break points at the if statement for StabilityChanged==null and see if it gets hit during test execution. – therealmitchconnors Jan 12 '12 at 22:50
  • something is wrong with those hit counts too, because there is no way the end bracket for the method is getting hit more frequently than the method declaration itself... something is wonky here... – therealmitchconnors Jan 12 '12 at 22:52
  • I agree the numbers look wrong and as such the data is probably misreporting the branch coverage as well. – Shaun Wilde Jan 13 '12 at 12:31
  • is it possible that the end bracket isn't hit when an exception is thrown? – Erik Aronesty Dec 11 '19 at 18:35