Settings variable values in a Moq Callback() call
Posted
by Adam Driscoll
on Stack Overflow
See other posts from Stack Overflow
or by Adam Driscoll
Published on 2010-03-22T18:56:48Z
Indexed on
2010/03/22
19:01 UTC
Read the original article
Hit count: 561
I think I may be a bit confused on the syntax of the Moq Callback methods. When I try to do something like this:
IFilter filter = new Filter();
List<IFoo> objects = new List<IFoo> { new Foo(), new Foo() };
IQueryable myFilteredFoos = null;
mockObject.Setup(m => m.GetByFilter(It.IsAny<IFilter>())).Callback( (IFilter filter) => myFilteredFoos = filter.FilterCollection(objects)).Returns(myFilteredFoos.Cast<IFooBar>());
This throws a exception because myFilteredFoos
is null during the Cast<IFooBar>()
call. Is this not working as I expect? I would think FilterCollection
would be called and then myFilteredFoos
would be non-null and allow for the cast.
FilterCollection
is not capable of returning a null which draws me to the conclusion it is not being called. Also, when I declare myFilteredFoos
like this:
Queryable myFilteredFoos;
The Return call complains that myFilteredFoos may be used before it is initialized.
© Stack Overflow or respective owner