chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes

I can mock successfully an Interface when it contains only properties and methods; but when adding an event declaration it causes an error.

Code of the class that is tested:
Public Class EcranControleur
   Implements IEcran

   Private WithEvents monAfficheur As IEcran
   Public Sub New(ByVal afficheur As IEcran)
      monAfficheur = afficheur    ' => CAUSES ERROR BECAUSE OF THE "WithEvents"
   End Sub
End Class

The error details in NUnit-gui gives me :
at TypeMock.Mock.a(String A_0, Object[] A_1, Object A_2, Object A_3, Int32 A_4)
at ao.a(String A_0, Object[] A_1, Object A_2, Object A_3)
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Object[] A_4)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Object p1)
at MockIEcran.add_DébutCréationGrille(DébutCréationGrilleEventHandler )
at Stepware.Sudoku.Logique.EcranControleur.set_monAfficheur(IEcran WithEventsValue)
at Stepware.Sudoku.Logique.EcranControleur..ctor(IEcran afficheur) in D:\Dev\Sudoku2\IHM\EcranControleur.vb:line 11
at Stepware.Sudoku.Logique.EcranControleurTest.DébutTest() in D:\Dev\Sudoku2\IHM\EcranControleurTest.vb:line 26

The issue seems to be an unexpected call to "MockIEcran.add_DébutCréationGrille" when it tries to add a reference to an event handler.

How can I do?

Thanks a lot.
asked by bb (1.3k points)

2 Answers

0 votes
When you mock an interface, there is NO implementation to any method. This includes methods that are created for you by the compiler. As this is the case the Mock is marked as 'Strict' (Mock.Strict and Mock.StaticStrict) and any call to an unexpected method results in the test failing.
To solve this you can do one of the following:

1. Allow calls mock.ExpectAlways("add_DébutCréationGrille");
2. Mock a concrete class and not the interface.
answered by scott (32k points)
0 votes
It's OK ! :D

And thanks for TypeMock framework !
answered by bb (1.3k points)