0

I'm parsing a date and throwing an exception. Aside from verifying an invalid date format throws an exception, I'm not sure what other branch to hit.

I did try Exception vs ArgumentException, both had the same result.

enter image description here

enter image description here

I am pretty new to unit test branch coverage, so any guidance would be appreciated.

bgraham
  • 1,939
  • 1
  • 10
  • 17
  • that means you need a Test that will Assert if that exception is thrown by passing invalid data to it. – madoxdev Feb 18 '19 at 14:53
  • I think I have that test. I added it for reference. It says 1 out of 2 branches are covered. I'm just not sure what the 2nd branch could be. – bgraham Feb 18 '19 at 14:56
  • how about a valid date time passed? That is another branch of the same thing – madoxdev Feb 18 '19 at 14:57
  • There shouldn't be any branching in that `throw` statement, unless there's something funky going on with either `request` or `AppTime`. – Lasse V. Karlsen Feb 18 '19 at 14:59
  • Yep, I have a couple of those too. You can see from the picture, the if block has 3 hits, but the throw statement just has one hit. The other two tests have valid dates and test other parts of the method. – bgraham Feb 18 '19 at 14:59
  • @LasseVågsætherKarlsen Yes thats why I'm confused. I'm not sure how there can be branches here. The request is an object with only fields. ApptTime is just a string with a getter and setter. Super standard. – bgraham Feb 18 '19 at 15:02
  • Could it be that you're running in Release mode and it's confused about where the branches really are due to an optimisation somewhere? – Jon Hanna Feb 18 '19 at 17:49
  • Thats a pretty good thought! I think it had to do with async code, but that could be a viable alternative – bgraham Feb 18 '19 at 18:12

1 Answers1

0

So the issue turns out to be with async code. Here's where I got the info:

https://github.com/OpenCover/opencover/issues/657

Basically, the compiler generates some extra code in an async method, and OpenCover looks at the compiled code. I was not able to figure out an optimal solution.

In this case, I was able to return the Task instead of awaiting it in this service. That was removed the shadow branch from the code coverage.

In another case, I just had to add another null check or two to get above code coverage requirements.

bgraham
  • 1,939
  • 1
  • 10
  • 17