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
client
amazon-ec2
react-virtualized
spring-cloud-stream
cmd
paw-app
jpeg
win32gui
icloud
cross-browser
ios-charts
node-pdfkit
midi
systemc
google-static-maps
firefox-webextensions
amazonsellercentral
paging
intentfilter
mps
excel-2007
telerik-reporting
tibco-mdm
crosstab
wpfdatagrid
brunch
typo3-6.2.x
log4js-node
fabric8
stringtemplate
commit
force-layout
environment-modules
starteam
theano.scan
impersonation
elasticsearch-plugin
sas-visual-analytics
sfdc
gulp-sourcemaps
jvm-languages
password-encryption
unixodbc
qcombobox
chain-builder
upstart
nativeapplication
magma
memory-alignment
jspdf-autotable
hittest
parallel-data-warehouse
spring-android
libpng
vmware-tools
mongocsharpdriver
plottable.js
player
ipconfig
revolution-r
ios8-today-widget
lemon
riak-cs
mono-embedding
ami
system32
web-controls
pretty-print
block-device
ng-animate
has-many-through
xceed-datagrid
picturefill
intentservice
mcts
jsctypes
gridfs
dir
coverflow
padarn
random-seed
smtp-auth
broken-links
getmessage
dotnethighcharts
boost-filesystem
fireworks
vdsp
sql-server-profiler
imac
cxxtest
meego
webkit.net
zend-tool
eqatec
avatar
genealogy
misv

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App