Suspend moq throws confusing events

We have been using Moq for two months now. However, there is a problem that cannot be solved somehow.

In the visual studio, all the tests went well. The build server runs several tests. What they have in common is that they use the raise method to create an event. Our build server tests obfuscated what is good to find obfuscation errors. Each "normal" wait is like "Setup (something) .Returns (something)" works. Only the boost event does not fire. stacktrace is as follows:

MESSAGE: Test method Ade.Graphic.Presenter.Test.RoutingEngineTest.TestRouteOverLadderLinesWithFbd threw exception: System.ArgumentException: Could not locate event for attach or detach method Void αœ€(ᦜ[ᒈ]). +++++++++++++++++++ STACK TRACE: bei Moq.Extensions.GetEvent[TMock](Action`1 eventExpression, TMock mock) bei Moq.Mock`1.Raise(Action`1 eventExpression, EventArgs args) bei Ade.Graphic.Presenter.Test.RoutingEngineTest.TestRouteOverLadderLinesWithFbd() 

Code for this:

 documentEventHandler.Raise(stub => stub.DocumentChanged += null, new DocumentChangeEventArgs(DocumentChangeTypes.ViewUpdate)); 

We don’t know what is the difference between the above code and this

 eventHandler.SetupGet(stub => stub.DocumentChangeNotify).Returns(documentEventHandler.Object); 

because this code is working fine.

Has anyone had the same problem, or at least can tell what the difference is?

+1
events moq obfuscation
source share
1 answer

The error (not sure how it is not tested) is probably due to the fact that events (i.e. DocumentChanged) are actually generated as 2 accessors: add_DocumentChanged and remove_DocumentChanged. This is similar to the properties that get and set accessors have.

What the obfuscator apparently renames is add_DocumentChanged and remove_DocumentChanged. However, looking at the moq source code, I see that moq relies on an event accessory, keeping the same name:

  var ev = addRemove.DeclaringType.GetEvent( addRemove.Name.Replace("add_", string.Empty).Replace("remove_", string.Empty)); 

ev == null in this case, which causes an error.

In your second examples, you are using delegates that are not broken into add_ and remove_ accessors.

You should probably not mess things up.

+2
source share

All Articles