Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Passing expression trees into Moq
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
benny  
View profile   Translate to Translated (View Original)
 More options Sep 22 2011, 4:59 am
From: benny <ceddlybu...@googlemail.com>
Date: Thu, 22 Sep 2011 01:59:08 -0700 (PDT)
Local: Thurs, Sep 22 2011 4:59 am
Subject: Passing expression trees into Moq
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 in .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+";


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Cazzulino  
View profile  
 More options Sep 22 2011, 10:46 am
From: Daniel Cazzulino <dan...@cazzulino.com>
Date: Thu, 22 Sep 2011 11:46:28 -0300
Local: Thurs, Sep 22 2011 10:46 am
Subject: Re: [Moq] Passing expression trees into Moq

Expression.Return(Expression.Label(),

that's wrong. you're not returning a label.

best way to go about this is to create the expression lambda with C# (v =>
v.Bar) and inspect the resulting expression tree. That's what you should be
creating next with expression API. There won't be a label there for sure.

/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1
425.329.3471


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
benny  
View profile  
 More options Sep 27 2011, 4:39 am
From: benny <ceddlybu...@googlemail.com>
Date: Tue, 27 Sep 2011 01:39:10 -0700 (PDT)
Local: Tues, Sep 27 2011 4:39 am
Subject: Re: Passing expression trees into Moq
Thanks, I like the idea of creating an expression lamba normally and
then inspecting the expression tree that gets created. I'll give it a
go.

On Sep 22, 3:46 pm, Daniel Cazzulino <dan...@cazzulino.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
benny  
View profile  
 More options Oct 11 2011, 8:25 am
From: benny <ceddlybu...@googlemail.com>
Date: Tue, 11 Oct 2011 05:25:24 -0700 (PDT)
Local: Tues, Oct 11 2011 8:25 am
Subject: Re: Passing expression trees into Moq
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]>(Window sFormsApplication1.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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »