fakeiteasy


Converting an MOQ property setup to FakeItEasy


In one of the "builders" we have following method that uses MOQ SetupGet. I tried re-writing it to use FakeItEasy, but there does not seem to be a way to do it, as WithReturnType provides only generic version, where type has to be known up front.
There is no non-generic version, that takes in actual type determined at the run time.
Granted I've spent whole 30 min. on FakeItEasy, so there might be a better way...
Original method with MOQ
public void With<TProperty>(Expression<Func<TCommand, TProperty>> expression, TProperty value)
{
CommandFake.SetupGet(expression).Returns(value);
}
With my limited knowledge of expressions I tried re-writing it to use FakeItEasy but I cannot set the return value.
public void With<TProperty>(Expression<Func<TCommand, TProperty>> expression, TProperty value)
{
var methodName = string.Empty;
var e = (LambdaExpression) expression;
if (e.Body.NodeType == ExpressionType.MemberAccess)
{
var memberInfo = ((MemberExpression) e.Body).Member;
if (memberInfo.MemberType == MemberTypes.Property)
{
methodName = "get_" + memberInfo.Name;
}
//A.CallTo(CommandFake).Where(call => call.Method.Name == methodName)
// .WithReturnType<>()
}
//CommandFake.SetupGet(expression).Returns(value);
}
Are there any other ways to do this? I am not stuck on this method signature for With.
This would also work
public void With(Expression<Action<TCommand>> expression)
so that one could do
With(x=>x.someProp=someValue)
I'm not a Moq user, but it looks like SetupGet just defines an action on a property get. This seems to be equivalent to A.CallTo in FakeItEasy.
Is there a reason not to just use
A.CallTo(() => CommandFake.TheProperty).Returns(value));
?
If you really want to be able to call something like
With(f => f.TheProperty, 7);
You could (working from your example) use
public void With<TProperty>(Expression<Func<TCommand, TProperty>> expression, TProperty value)
{
var methodName = string.Empty;
var e = (LambdaExpression)expression;
if (e.Body.NodeType == ExpressionType.MemberAccess)
{
var memberInfo = ((MemberExpression)e.Body).Member;
if (memberInfo.MemberType == MemberTypes.Property)
{
methodName = "get_" + memberInfo.Name;
}
A.CallTo(CommandFake).Where(call => call.Method.Name == methodName)
.WithReturnType<TProperty>()
.Returns(value);
}
}
Or even consider just using the property's method outright, instead of the name:
public void With<TProperty>(Expression<Func<TCommand, TProperty>> expression, TProperty value)
{
var e = (LambdaExpression)expression;
if (e.Body.NodeType == ExpressionType.MemberAccess)
{
var memberInfo = ((MemberExpression)e.Body).Member;
if (memberInfo.MemberType == MemberTypes.Property)
{
A.CallTo(CommandFake).Where(call =>
call.Method == ((PropertyInfo)memberInfo).GetMethod)
.WithReturnType<TProperty>()
.Returns(value);
}
}
}

Related Links

Converting an MOQ property setup to FakeItEasy
Howto loop OrderedAssertions in FakeItEasy 2
FakeItEasy setting property with no get accessor?
FakeItEasy mocked method won't return object, instead nullReferenceException
How to specify the class to use when faking a property?
How can I fake a Class used insite SUT using FakeItEasy
FakeItEasy expectation fail against HashSet comparisons
FakeItEasy AssignsOutAndRefParameters - lazily?
How do I find the underlying Type of a Fake object returned from FakeItEasy?
FakeItEasy ReturnsLazily with out parameter
how to verify that a method was called with an argument of a specific type
How to fake delegates with FakeItEasy
FakeItEasy & “params” arguments
Using Expression Trees as an argument constraint
How to update a property on a parameter using FakeItEasy

Categories

HOME
yii2
cookies
activiti
relay
in-app-purchase
add
facebook-messenger-bot
elasticsearch-hadoop
nano-server
windows-azure-storage
circular-dependency
quartz-scheduler
vaadin7
opentracing
introduction
tar
sylius
tapestry
core-text
bootstrap-tour
captiveportal
microsoft-sync-framework
qhull
chromebook
wpfdatagrid
internet-explorer-9
fifo
ping
ejabberd-module
bosh
grails-3.1
catalog
dartium
serve
event-driven
withings
ansible-playbook
cookiecutter-django
mesos-chronos
galen
sfdc
google-closure
http-redirect
reactive-cocoa-5
.net-4.6.2
azure-application-gateway
filepicker
pubmed
datastax-startup
csound
zendesk-app
root-framework
bstr
player
actionbardrawertoggle
hill-climbing
captivenetwork
bluegiga
phpcas
iad
msys
google-places
muse
web-controls
c++03
kineticjs
system.reflection
asp.net-dynamic-data
file-copying
code-access-security
valuechangelistener
message-driven-bean
viadeo
industrial
multiprocessor
ftps
dotnetnuke-5
seed
sublist
funscript
ohm
removeclass
assembly-loading
php-parser
gnu-prolog
psi
appendto
infrastructure
web-architecture
nintendo-ds
pascal-fc

Resources

Encrypt Message