How to refactor static constructor to enable unit / acceptance testing

Viewed 119

In my acceptance test for a service, I'm setting up the testee as follows:

var dataService = A.Fake<IDataService>();
this.domainRepository = A.Fake<IDomainRepository>();
var dbContextArticles = A.Fake<IDbContextArticles>();

this.testee = new DomainService(dataService, this.domainRepository, dbContedbContextArticles xtArtikel);

This setup unfortunately fails, because.

  1. The DomainService class inherits from BaseDataService.
  2. BaseDataService has a static constructor that looks like this:
    DbContext.LoadTranslations();
  3. And DbContext.LoadTranslations(); looks like this:
    using (DomainEntities entities = new DomainEntities(DbBase.ConnectionString)) { ... }

So when I create an instance of DomainService it fails, because of couse in my acceptance test there is no connection string defined since I don't want to use the real database.

According to a comment, the translations have to be loaded in the static constructor or otherwise it gets loaded 8 times when searching for an article...

  • Do you have any proposals how I can refactor this so that the acceptance test uses a fake context?

I tried introducing a ContextFactory but the problem is, that the static constructor of BaseDataService calls the static method DbContext.LoadTranslations() so I can't inject a IContextFactory into DbContext because this constructor isn't even called...

Thanks for any advice.

0 Answers
Related