fakeiteasy


How to specify the class to use when faking a property?


I find that I am often faking IDbSet from Entity Framework. I typically have an interface like this:
public interface IContext : IDisposable
{
IDbSet<Cat> Cats { get; set; }
IDbSet<Dogs> Dogs { get; set; }
}
Which I fake like this:
IContext context = A.Fake<IContext>();
context.Cats = new FakeDbSet<Cat>();
context.Dogs = new FakeDbSet<Dogs>();
Those last two lines of code are becoming a pain.
FakeDbSet is a custom class which we always want to use instead the FakeItEasy fake.
Is there some way I can just tell FakeItEasy that anywhere it sees an IDbSet, use FakeDbSet?
Is there some way I can just tell FakeItEasy that anywhere it sees an IDbSet, use FakeDbSet?
Not in this way, no. There are custom Dummies, whose powers are vastly improved in the upcoming 2.0 release, but currently properties do not return Dummies when they can return a fakeable type (see issue 156 for probably way too much information on this). Otherwise, you'd be all set.
Failing that, the best option really is to use reflection to look at the properties' return types and set the value accordingly.
You could use the newly-expanded IFakeConfigurator powers in the 2.0 betas as a hook to enable this behaviour, so every fake that's created would have its properties examined and the desired FakeDbSet added.
Something like this:
public class PropertiesUseFakeDbSetFakeConfigurator : FakeConfigurator<IContext>
{
protected override void ConfigureFake(IContext fakeObject)
{
var fakeObjectType = fakeObject.GetType();
var properties = fakeObjectType.GetProperties(
BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.GetProperty |
BindingFlags.SetProperty);
foreach (var propertyInfo in properties)
{
var propertyType = propertyInfo.PropertyType;
if (propertyType.IsGenericType &&
propertyType.GetGenericTypeDefinition() == typeof (IDbSet<>))
{
var typeInTheSet = propertyType.GetGenericArguments()[0];
var fakeDbSetType = typeof (FakeDbSet<>).MakeGenericType(typeInTheSet);
var fakePropertyValue = Activator.CreateInstance(fakeDbSetType);
propertyInfo.SetValue(fakeObject, fakePropertyValue, null);
}
}
}
}
would make this pass:
[Test]
public void Properties_should_be_FakeDbSets()
{
IContext context = A.Fake<IContext>();
Assert.That(context.Cats, Is.InstanceOf<FakeDbSet<Cat>>());
Assert.That(context.Dogs, Is.InstanceOf<FakeDbSet<Dog>>());
}
If you have several classes like IContext in your solution, you may want to implement IFakeConfigurator directly, rather than using FakeConfigurator<T>. It requires a little more work, but provides a more sophisticated way to determine which fakes are configured. FakeConfigurator<IContext> will only configure faked IContexts.

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
xamarin
variables
cloud
gerrit
image-processing
c#-4.0
path-finding
plone
react-virtualized
react-router
octobercms
webstorm
youtube-dl
dxl
flyway4
datastax-java-driver
bs4
qore
synchronization
primary-key
facebook-php-sdk
reveal.js
midi
floating-action-button
quicklook
apache-cayenne
task
ups
jsprit
caml
telephony
django-admin
django-cms
crystal-reports-2008
jtextfield
conemu
facebook-instant-articles
underflow
greendao
libssl
vision
flink-streaming
.net-4.0
perlin-noise
lxd
gammu
io-redirection
lto
space-complexity
event-driven
android-browser
particles.js
preconditions
email-templates
ensembles
sql-server-agent
password-encryption
thin
skeleton-css-boilerplate
osx-mavericks
react-scripts
qsslsocket
recursive-datastructures
setter
worker-thread
knockout-components
nsarray
eventkit
libpng
elements
mikroc
dataview
associative-array
cloudbees
freedesktop.org
hsv
marching-cubes
cannon.js
feedback
php-ci
splash
createprocessasuser
nsight
rake-task
oxwall
pick
zend-route
openexr
bundles
datagridviewcolumn
mcts
farseer
ril
eventual-consistency
anonymous-methods
pysimplesoap
php-parser
window-management
vdsp
heartbeat
blackberry-playbook
infobox
django-tagging
xtype
locationlistener
yetanotherforum
netbeans-7.1
oncheckedchanged
locate
dentrix
routedevent
premature-optimization
private-members
django-notification
lzh

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