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
ibm-bluemix
netbeans
urbancode
azure-data-factory
tinymce
angular-material
relayjs
android-4.4-kitkat
getelementsbytagname
yarn
leon
rubygems
virtualization
modelica
caml
http-status-code-504
text-rendering
jprofiler
sylius
language-agnostic
zurb-foundation-6
firebase-crash-reporting
qhull
wtx
subdomains
objectanimator
google-qpx-express-api
http-get
framemaker
accelerate-framework
amazon-kinesis-kpl
host
serve
azure-ml
greenrobot-eventbus
broadcastreceiver
texmaker
fakeiteasy
turbogears
unixodbc
businessworks
thin
segment
wptoolkit
network-flow
termination
gabor-filter
pg-dump
namecoin
zendesk-app
smart-table
static-ip-address
elements
fody
xml-attribute
multiple-regression
superstack
reactive-banana
collapse
jmeter-maven-plugin
cakephp-3.1
event-bubbling
cisco-ios
block-device
camanjs
knuth
gui-test-framework
neolane
xojo
responsive-slides
typeof
navigationservice
jubula
transcoding
fmod
mcts
geos
ms-project-server-2010
qt-faststart
padarn
pushbackinputstream
mqx
trusted
word-processor
pysimplesoap
newtonscript
ohm
eclipse-templates
coercion
cinema-4d
self-extracting
android-sdk-2.1
lang
visitor-statistic
yslow
infrastructure
webkit.net
audio-capture
adrotator
privilege
project-hosting
ntvdm.exe

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App