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
cakephp
xamarin
multithreading
gremlin
mockito
pycharm
fme
mean-stack
gis
autotools
python-unittest
gnupg
n-gram
hapi
pugjs
apache-metamodel
nodatime
java-3d
arabic
buildbot
applozic
typo3-6.2.x
jaxb2
lightswitch-2013
iframe-resizer
uisplitview
google-qpx-express-api
column-family
protovis
azure-sql-database
environment-modules
serverside-rendering
service-discovery
reportbuilder
hp-ux
texmaker
heightmap
segment
babel-core
xml-documentation
smartcontracts
dds
worker-thread
diagnostics
pg-dump
libusb-win32
font-size
np-complete
capacity
jspdf-autotable
color-profile
dwscript
vtigercrm
eventkit
jquery-filter
wdf
removechild
elements
url-masking
fody
py2app
etsy
qpid
hover-over
design-by-contract
ptrace
wapiti
browser-link
rtbkit
separator
lemon
python-3.2
qdialog
django-unittest
citrus-pay
pundit
map-projections
deis
composite
p2
pretty-print
bridge.net
apache-commons-net
kineticjs
csquery
zend-route
terminfo
xojo
eol
system.net.webexception
java.nio.file
pyhdf
farseer
geos
dexterity
file-locking
buster.js
google-email-migration
first-responder
onsubmit
coercion
appendto
krl
celltable
h.323
zend-translate
gamma
ncqrs
iweb
virtual-functions
zend-decorators
django-notification

Resources

Encrypt Message