3

I am writing unit tests using Autofixture and FakeItEasy for little tasks that are talking to the database through NHibernate. My test cases also include scenarios where a given object is not found in the database, and therefore I'd like to emulate that the queries return null.

Now, I managed to achieve this by using the following calls:

    var _session = _fixture.Freeze<ISession>();
    A.CallTo(_session).WithReturnType<Foo>().Returns(null);
    A.CallTo(_session).WithReturnType<Bar>().Returns(null);
    // ~10 more of these with just different types defined

but I was wondering if there is a way of setting this up as the default behaviour - so that all calls to _session with a generic parameter will return null by default?

blas3nik
  • 1,381
  • 11
  • 21
  • FWIW, [AutoFixture.AutoMoq](https://www.nuget.org/packages/AutoFixture.AutoMoq) has `AutoConfiguredMoqCustomization`, and [AutoFixture.AutoNSubstitute](https://www.nuget.org/packages/AutoFixture.AutoNSubstitute) has `AutoConfiguredNSubstituteCustomization`. Both give you the option of using AutoFixture as a value creator for Test Doubles. This feature doesn't exist for FakeItEasy. – Mark Seemann Apr 12 '16 at 17:04

2 Answers2

3

No, I don't think there is.Looks like you'll have to configure each type in turn for now. Consider proposing it as an enhancement!

Update I see you created issue 648 to track this. Thanks!

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
1

This is an area where AutoFixture and FakeItEasy overlap in functionality. FakeItEasy has built-in support for creating dummy objects to use in your tests, and so does AutoFixture. The main difference between the two is that FakeItEasy doesn't come up with test values to assign to those objects, while AutoFixture is all about providing sensible defaults that are good enough for most use cases.

When AutoFixture has to provide an instance for an interface or an abstract class, it simply delegates the creation to the configured mocking library, in this case FakeItEasy. This means that if you want to change the way fake objects are created, you'll need to configure FakeItEasy, not AutoFixture.

Unfortunately, as @BlairConrad pointed out, FakeItEasy doesn't currently provide a way to disable dummy creation for specific fake objects.

Community
  • 1
  • 1
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154