3

I have the following xUnit unit test that is throwing nullReferenceException.

So I decided to debug. However, when I debug my test fails before even it hits the first break point.

How do I fix this?

enter image description here

Dave
  • 14,618
  • 13
  • 91
  • 145
socialMatrix
  • 1,423
  • 5
  • 20
  • 36

5 Answers5

4

I had a similar issue in Visual Studio 2017. The unit test project was added automatically by the framework. I read many blogs, and nothing was able to solve my issue. Finally I got it to work using the following approach: At the Solution Level, Right Click => Properties=> Set them to Debug mode.

4

Before you run your unit tests the next time, hit CTRL-D, E to bring up the Debug | Exceptions window. To make it quicker, just put a check next to CLR Exceptions in the Thrown column. Now run. Hopefully, execution will break at the source of your NullReferenceException.

Dave
  • 14,618
  • 13
  • 91
  • 145
1

Seems like you've got an error that occurs before your test method. I'd check the constructor. It might be the test initialization code there.

Assaf Stone
  • 6,309
  • 1
  • 34
  • 43
0

Do you have methods with the xUnit-equivalents of MSTest's [ClassInitialize] or [TestInitialize] attributes? If one of those is throwing your NullReferenceException, that could be why it's not hitting your breakpoint.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
0

I am not familiar with xUnit, but it sounds like your test class either:

  1. Fails in init method or constructor

  2. Can not be created from its DLL.

Make sure you ate copying any dependant DLLs and resources to test execution dir on static class int, prior to constructor of class bring called.

Danny Varod
  • 17,324
  • 5
  • 69
  • 111