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!
// 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+";
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
On Thu, Sep 22, 2011 at 05:59, benny <ceddlybu...@googlemail.com> wrote: > 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!
> // 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+"; > }
> 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
> On Thu, Sep 22, 2011 at 05:59, benny <ceddlybu...@googlemail.com> wrote:
> > 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!
> > // 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+";
> > }
// 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
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.