Moq和反射,将动态生成的表达式树/lambda传递给moq

发布于 2024-12-03 15:05:20 字数 1031 浏览 0 评论 0原文

是否可以编写如下代码。我正在尝试将 Moq 与我正在反思的对象一起使用,作为测试框架的一部分。下面的代码引发了来自 Moq 的“未处理的表达式类型:'Goto'”异常,我猜这是期待不同的东西。看起来应该可以用!

    private void button1_Click(object sender, EventArgs e)
    {
        Ifoo  = foo Foo();

        // Create input parameter for lambda
        ParameterExpression value = Expression.Parameter(typeof(IFoo), "value");

        // create return statement for lambda
        Expression setupProperty = Expression.Return(Expression.Label(), Expression.Property(value, "Bar"), typeof(string));

        // convert expression to lambda (should now be the equivalent of "v => v.Bar")
        var func = Expression.Lambda<Func<IFoo, string>>(setupProperty, value);//.Compile();
        //string s = func(foo); // this bit works fine if .Compile() is included

        var mockFoo = new Mock<IFoo>();

        mockFoo.SetupProperty(func); // exception thrown by moq here, obviously isn't exactly the same as "v => v.Bar"
        mockFoo.Object.Bar = "Burge+";
    }

谢谢!

Is it possible to write code like the following. I'm trying to using Moq with objects that I'm reflecting on as part of a testing framework. The code below raises a "Unhandled expression type: 'Goto'" exception from Moq, which I guess is expecting something different. It kind of looks like it should work though!

    private void button1_Click(object sender, EventArgs e)
    {
        Ifoo  = foo Foo();

        // Create input parameter for lambda
        ParameterExpression value = Expression.Parameter(typeof(IFoo), "value");

        // create return statement for lambda
        Expression setupProperty = Expression.Return(Expression.Label(), Expression.Property(value, "Bar"), typeof(string));

        // convert expression to lambda (should now be the equivalent of "v => v.Bar")
        var func = Expression.Lambda<Func<IFoo, string>>(setupProperty, value);//.Compile();
        //string s = func(foo); // this bit works fine if .Compile() is included

        var mockFoo = new Mock<IFoo>();

        mockFoo.SetupProperty(func); // exception thrown by moq here, obviously isn't exactly the same as "v => v.Bar"
        mockFoo.Object.Bar = "Burge+";
    }

Thanks!

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

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

发布评论

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

评论(1

带刺的爱情 2024-12-10 15:05:20

好的,这是可能的,这是更正后的代码。

// Create input parameter for lambda 
ParameterExpression value = Expression.Parameter(typeof(IFoo), "value"); 

// create return statement for lambda 
Expression setupProperty = Expression.Property(value, "Bar"); 

// convert expression to lambda (should now be the equivalent of "v => v.Bar") 
var func = Expression.Lambda<Func<IFoo, string>>(setupProperty, value);

var mockFoo = new Mock<IFoo>(); 
mockFoo.SetupProperty(func); // this works now
mockFoo.Object.Bar = "Burge+"; 

我通过使用下面的代码从 lambda 创建一个表达式来研究这个问题

Expression<Func<IFoo, string>> setupBar = v => c.Bar;

,然后在 vs 2010 中的调试器中查看它。表达式有一个“调试视图”,它显示表达式的文本表示形式,因此可以添加监视那个或类似的东西。 时,上面的内容就出现了

 .Lambda #Lambda1<System.Func`2[WindowsFormsApplication1.IFoo,System.String]>(WindowsFormsApplication1.IFoo
 $v) {
   $v.Bar
 }

当我查看这个并尝试找出表达式会产生这个结果

,然后创建了一个表达式并在调试器中对其进行了比较。对我来说有趣的是,虽然这个表达式返回一个值,但没有赋值或返回语句。我想这一定是隐含的。

Ok, this is possible, here is the corrected code.

// Create input parameter for lambda 
ParameterExpression value = Expression.Parameter(typeof(IFoo), "value"); 

// create return statement for lambda 
Expression setupProperty = Expression.Property(value, "Bar"); 

// convert expression to lambda (should now be the equivalent of "v => v.Bar") 
var func = Expression.Lambda<Func<IFoo, string>>(setupProperty, value);

var mockFoo = new Mock<IFoo>(); 
mockFoo.SetupProperty(func); // this works now
mockFoo.Object.Bar = "Burge+"; 

I investigated this by creating an expression from a lambda using the code below

Expression<Func<IFoo, string>> setupBar = v => c.Bar;

I then looked at this in the debugger in vs 2010. Expressions have a "Debug View" that shows a text representation of the expression so it is possible to add a watch on that or something similar. The above comes out as

 .Lambda #Lambda1<System.Func`2[WindowsFormsApplication1.IFoo,System.String]>(WindowsFormsApplication1.IFoo
 $v) {
   $v.Bar
 }

I looked at this and tried to work out what Expressions would make this, then created an expression and compared it in the debugger.

The interesting thing for me is that although this expression returns a value there is no assignment or return statement. I guess this must be implicit somehow.

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