Pex 和 Moq 可以一起工作吗?

发布于 2024-10-08 02:40:13 字数 112 浏览 2 评论 0原文

有人试过这个吗?

我喜欢起订量,也喜欢 pex 正在做的事情,但还没有一起尝试过。我认为在大多数情况下我更喜欢使用起订量而不是摩尔,但我很好奇是否有人遇到了障碍?

他们玩得好吗?

Has anyone tried this?

I like moq and i like what pex is doing, but haven't tried them together. I'd prefer to use moq over moles in most cases I think but am curious to see if anyone has hit roadblocks?

Do they play nice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

递刀给你 2024-10-15 02:40:13

虽然我没有尝试过,但 Pex 和 Moq 应该像老朋友一样相处。

虽然 Pex 和 Moq 之间的拦截技术不同(Pex 使用 ProfilerAPI 解释 MSIL 指令;Moq 使用 DynamicProxy 动态派生类),但 Moq 源代码中的引用表明它的设计目的是防止 Pex 会出现的重入问题干扰起订量。

根据原始的Pex研究论文,你可以装饰你的具有控制何时使用 Pex 重写器的属性的代码。

来自 Moq 源代码

internal static MethodCall<T> Setup<T>(Mock mock, Expression<Action<T>> expression, Func<bool> condition) where T : class
{
    return PexProtector.Invoke(() =>
    {
       var methodCall = expression.ToMethodCall();
       var method = methodCall.Method;
       var args = methodCall.Arguments.ToArray();
       ThrowIfNotMember(expression, method);
       ThrowIfCantOverride(expression, method);

       var call = new MethodCall<T>(mock, condition, expression, method, args);
       var targetInterceptor = GetInterceptor(methodCall.Object, mock);
       targetInterceptor.AddCall(call, SetupKind.Other);

       return call;
     });   
 }

PexProtector定义为:

 internal sealed class __ProtectAttribute : Attribute
 {
 }

 namespace Moq
 {
    [__Protect]
    [DebuggerStepThrough]
    internal static class PexProtector
    {
        public static void Invoke(Action action)
        {
           action();
        }

        public static T Invoke<T>(Func<T> function)
        {
           return function();
        }
    }
 }

Although I haven't tried, Pex and Moq should get along like old friends.

While the interception techniques between Pex and Moq are different (Pex uses the ProfilerAPI to interpret MSIL instructions; Moq uses DynamicProxy to dynamically derive classes) there are references in the Moq source code that suggest it was designed to prevent re-entrance problems where Pex would interfere with Moq.

According to the original research paper for Pex, you can decorate your code with attributes that control when the Pex rewriter is used.

From the Moq source code:

internal static MethodCall<T> Setup<T>(Mock mock, Expression<Action<T>> expression, Func<bool> condition) where T : class
{
    return PexProtector.Invoke(() =>
    {
       var methodCall = expression.ToMethodCall();
       var method = methodCall.Method;
       var args = methodCall.Arguments.ToArray();
       ThrowIfNotMember(expression, method);
       ThrowIfCantOverride(expression, method);

       var call = new MethodCall<T>(mock, condition, expression, method, args);
       var targetInterceptor = GetInterceptor(methodCall.Object, mock);
       targetInterceptor.AddCall(call, SetupKind.Other);

       return call;
     });   
 }

PexProtector is defined as:

 internal sealed class __ProtectAttribute : Attribute
 {
 }

 namespace Moq
 {
    [__Protect]
    [DebuggerStepThrough]
    internal static class PexProtector
    {
        public static void Invoke(Action action)
        {
           action();
        }

        public static T Invoke<T>(Func<T> function)
        {
           return function();
        }
    }
 }
梦在深巷 2024-10-15 02:40:13

我没有让 pex amd moq 能够很好地协同工作,尽管那是很久以前的事了。 Pex 似乎迷失在 moq 创建的 Reflection.Emit / 动态代理内容中。

如果您需要与 pex 结合进行模拟,我建议您查看 Moles。总的来说,它是一个非常好的模拟框架,并且已经与 pex 捆绑在一起

i didnt get pex amd moq to work very well toghther, allthough that was a long time ago. Pex seemed to get lost in the Reflection.Emit / dynamic proxy stuff that moq creates.

i'd suggest looking at Moles if you need to do mocking in conjunction with pex. its a pretty nice mocking framework over all and is already bundled with pex

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文