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
bluetooth
isabelle
server
drivers
malloc
c#-2.0
icloud
ebean
networkx
azure-storage-tables
webrequest
contact
windows-azure-storage
jplayer
spring-tool-suite
vb.net-2010
phaser
ibm-odm
finite-automata
connection-string
conemu
ml
scriptcs
arabic
hammerspoon
numerical-methods
sparse-matrix
fish
cas
internet-explorer-9
rst2pdf
xacml
instant-messaging
sql-server-2012-express
directx-10
airconsole
xmlreader
form-data
cloud-code
google-api-nodejs-client
force-layout
libvpx
sql-server-agent
grid.mvc
botbuilder
s
parentheses
azure-application-gateway
powercli
veracode
tropo
kbuild
background-service
gridpane
savon
skobbler-maps
multiple-regression
jwplayer7
holder.js
essence
suffix-tree
pagerank
revolution-r
natvis
map-projections
appfabric-cache
modalpopup
wordpress-theme-customize
neolane
poller
fmod
mcts
floating-point-precision
c18
sitemesh
mylyn
referrer
hamiltonian-cycle
path-separator
libstdc++
quickdialog
bluepill
armcc
netdna-api
locationlistener
file-comparison
inotifycollectionchanged
user-friendly
nintendo-ds
virtual-functions
thread-local-storage
putchar
genealogy
document-conversion

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