使用最小起订量引发混淆事件会引发错误

使用最小起订量引发混淆事件会引发错误

本文介绍了使用最小起订量引发混淆事件会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经使用Moq两个月了.但是,有一个问题无法解决.

We have using Moq for two month now. However there is a problem which can not solve somehow.

在Visual Studio中,所有测试都成功完成.在构建服务器上,有几次测试失败.他们的共同点是,他们使用"raise"方法引发事件.我们的构建服务器测试混淆了发现混淆错误的好处.每个像"Setup(something).Returns(something)"之类的正常"期望都有效.仅引发事件失败. stacktrace如下所示:

In visual studio all tests succeeded just fine. On the build server there are several tests which failed. What they have in common is, that they use the "raise" method to throw an event. Our build server tests obfuscated what is good to find obfuscation errors. Every "normal" expectation like "Setup(something).Returns(something)" works. Only the raise event fails. the stacktrace looks like the following:

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()

此代码为:

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

我们不知道上面的代码和这个代码有什么区别

We have no idea what is the difference between the code above and this

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

因为此代码可以正常工作.

because this code works fine.

有人有同样的问题,或者至少可以说出区别是什么吗?

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

推荐答案

错误(不确定是否经过测试)可能来自以下事实:事件(即DocumentChanged)实际上是作为2个访问器生成的:add_DocumentChanged和remove_DocumentChanged.这类似于具有get和set访问器的属性.

The error comes probably (not sure as not tested) from 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 have the get and set accessors.

混淆器最可能做的就是重命名此add_DocumentChanged和remove_DocumentChanged.但是,查看moq源代码,我可以看到moq依赖于事件访问器保持相同的名称:

What the obfuscator did most probably is rename this add_DocumentChanged and remove_DocumentChanged. However, looking at the moq source code, I can see that moq relies on the events accessor keeping the same name:

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

ev == null在这种情况下,会引发错误.

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

在第二个示例中,您正在使用未细分为add_和remove_访问器的委托.

In your second examples, you're using delegates which are not broken down into add_ and remove_ accessors.

最好不要混淆事件.

这篇关于使用最小起订量引发混淆事件会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 19:42