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 IContext
s.