0

I use AutoFixture with my BDD tests.

I'm trying to write a fixture for a User class which in turn uses CentralConfiguration class. CentralConfiguration constructor looks like this:

public CentralConfiguration(
    IConfigurationRepository configurationRepository,
    ILogger logger)
{
   _logger = logger;
   _configuration = configurationRepository.Single();
   LogPropertyValues();
}

The second line in the constructor, although working fine when used by a user, throws "Sequence contains no elements" exception every time I try to build a fixture for tests. I even tried building a Configuration object manually and using

configuration.Single().Returns(myCustomObject)

but nothing changed (actually this line started throwing the same exception).

What am I doing wrong, and how can I circumvent this issue?

Nikos Baxevanis
  • 10,868
  • 2
  • 46
  • 80
Eedoh
  • 5,818
  • 9
  • 38
  • 62
  • Are you using an auto-mocking Glue Library as well? (e.g. AutoFixture.AutoMoq) – Nikos Baxevanis Jun 28 '16 at 05:00
  • 1
    If `CentralConfiguration` only depends on a single `Whatchamacallit`, then why don't you inject _that_ instead? [Injection Constructors should be simple](http://blog.ploeh.dk/2011/03/03/InjectionConstructorsshouldbesimple). – Mark Seemann Jun 28 '16 at 06:29

1 Answers1

0

Change CentralConfiguration to:

public CentralConfiguration(
    IConfiguration configuration,
    ILogger logger)
{
   _logger = logger;
   _configuration = configuration;
   LogPropertyValues();
}

Also consider removing the call to LogPropertyValues from the constructor. Injection Constructors should be simple.

Community
  • 1
  • 1
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736