0

We mainly use class injections theses days and that works great with unit test but there is some(a lot) of code that makes calls directly to the container and this results in problems while running tests.

In each test class I got a SetupData() method that will run once per class. This will add the data for this specific class test. This works fine as long as I do not run multiple (class) tests after each other. If I do that It will tell me that the container is locked.

private void SetupData()
{
    TestEntityviewCache evCache;
    if (!_dataIsSetup)
    {
        evCache = new TestEntityviewCache();
        IOC.Current.RegisterSingleton<IEntityViewCache>(evCache);

        EntityView.GetEntityViewListMethod = (key) => evCache.Get(key);
        EntityView.GetEntityViewListByIdMethod = (key) => evCache.Get(key);
    }
    evCache = IOC.Current.Get<TestEntityviewCache>();

    evCache.Update(EntityViewKey.Mappning, false, MyModelData.CreateMappning(), null);
    evCache.Update(EntityViewKey.Kontrakt, false, MyModelData.CreateKontrakt(), null);
    evCache.Update(EntityViewKey.Folder, false, MyModelData.CreateFolder(), null);
    evCache.Update(EntityViewKey.Kontaktgrupp, false, MyModelData.CreateKontaktGrupp(), null);
}

I have tried reseting or recreating the continer for each class test but I can´t find a solution?

The best solution would be to replace all direct IOC calls but that is to much work at this point.

4
  • 1
    "there is some(a lot) of code that makes calls directly to the container and this results in problems". Yep. Doing this isn't called an anti-pattern for nothing.
    – Steven
    Mar 5, 2021 at 16:36
  • The only solution I see is by refactoring this code to use constructor injection. You can't prevent the container from getting locked (for good reason).
    – Steven
    Mar 5, 2021 at 16:40
  • There is no way to do refactory :( , I was hoping I would reset or instanciate a new IOC for a specific test. I t souns like I will have to run tests individual and not in sequence. Its not fun but its better then not having any testa at all.
    – Banshee
    Mar 5, 2021 at 19:08
  • There is no way to do refactory - You are on your own then ;)
    – Fabio
    Mar 5, 2021 at 19:25

1 Answer 1

0

In this case I hade to run the tests in sequence.

2
  • How did this solve the problem? Jul 29, 2022 at 20:00
  • I don't really know why but it just worked while running them in sequence. Suspect that they do share some resource but haven't mange to find it.
    – Banshee
    Aug 23, 2022 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.