-1

I am testing a controler using Moq and I get a NullReferenceException when the method I'm testing raises an event. Any idea on how to solve this ?

Here is the test code :

       //Arrange
        mockNumeriseur.Setup(x => x.InitialiserNumerisation());
        mockCommande.Setup(x => x.ConnaitreStatut()).Returns(numerisationReussie);
        mockNumeriseur.Setup(x => x.Numeriser(false, It.IsAny<string>(), It.IsAny<IntPtr>()));

        //Act
        controleur.Numeriser(new IntPtr(), false, false, false);

        //Assert
        mockNumeriseur.Verify(x => x.InitialiserNumerisation(), Times.Once());
        mockCommande.Verify(x => x.ConnaitreStatut(), Times.Once());
        mockNumeriseur.Verify(x => x.Numeriser(false, It.IsAny<string>(), It.IsAny<IntPtr>()), Times.Once());

Here is the line that causes the issue :

ActiverBoutonsNumerisationUniqueEvent(this, new BooleanEventArgs(false));

BooleanEventArgs extends EventArgs to allow passing a boolean to the EventHandler.

Edit : I know what a NullReferenceException is, what I'm trying to figure out is why my unit tests cause one when this event is triggered. I think it might have something to do with Unity/Moq but I'm new to these libraries so I don't know where to start.

Lap
  • 31
  • 10
  • 5
    You can run unit tests in debug mode. – Kenneth K. Aug 02 '18 at 13:40
  • What happens in `ActiverBoutonsNumerisationUniqueEvent`? – Marco Forberg Aug 02 '18 at 13:40
  • 1
    I don't see any assignments to this event in test, are you sure there is one? And in all cases use null-propagation to avoid this kind of exception:ActiverBoutonsNumerisationUniqueEvent?.Invoke(this, new BooleanEventArgs(false)); – Anton Pavlov Aug 02 '18 at 13:55
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Camilo Terevinto Aug 02 '18 at 14:01
  • The test isn't meant ot verify that the event is triggered, it is meant to validate that methods from two classes are called for the right cases. ActiverBoutonsNumerisationUniqueEvent updates the view when a scan is either started or finished. I know I can run unit tests in debug mode, that's how I found out that this line causes a NullReferenceException. My code works perfectly in a normal case, it's when I run the test that it crashes. – Lap Aug 02 '18 at 14:02
  • I know what a NullReferenceException is, I just can't figure out why my tests cause one. – Lap Aug 02 '18 at 14:03
  • If you've debugged your test, you must know what is null that is not expected to be. I would guess that object is returned from a mocked call, so the conditions you specify in your test setup is not being satisfied by the call to the mocked object. – Lee Aug 02 '18 at 14:13
  • Yes, ActiverBoutonsNumerisationUniqueEvent is null for some reason, I declared it at the top of my class, what am I doing wrong ? Is there a way to declare it in my test setup or something ? – Lap Aug 02 '18 at 14:15
  • Thanks @AntonPavlov, null-propagation worked ! – Lap Aug 02 '18 at 14:35
  • It is null-conditional operator - ie call the stack only of value is not null. You can write it in classic way using var event = ActiverBoutonsNumerisationUniqueEvent; if (event != null) event(...); – Anton Pavlov Aug 02 '18 at 14:36

1 Answers1

0

Thanks to Anton Pavlov for the solution, I thought I'd post it here to make it easier if someone has the same problem.

Using null-propagation fixes the problem. Basically it verifies if ActiverBoutonsNumerisationUniqueEvent is null before triggering the event. If it's null, it doesn't trigger it. Which is ideal for my tests.

ActiverBoutonsNumerisationUniqueEvent?.Invoke(this, new BooleanEventArgs(false));
Lap
  • 31
  • 10