Share via

Keep dictionary state between the methods in a same class

pratap chandra 1 Reputation point
2022-06-14T22:57:15.457+00:00

I have below test class, Where I am defining _cacheExecutor dictionary at class level, and the purpose is mainly to cache the results with type T and test data as keys.

public class CodesAndGuidelinesTest : SchemaCache, IClassFixture<PostgreSqlResource>  
{         
    private readonly Dictionary<(Type, object), Task<IRequestExecutor>> _cacheExecutor = new();  
    public CodesAndGuidelinesTest(PostgreSqlResource resource) :base(resource) { }  

    [Fact]  
    public async Task Create_Name_Contains_Expression()  
    {  
        IRequestExecutor requestExecutor = await CreateSchemaAsync(CodesAndGuidelinesMockFixture.codeStandardGuidelines, _cacheExecutor);  
        .......  
        .......    
    }  

    [Fact]  
    public async Task Create_Name_NotContains_Expression()  
    {  
        IRequestExecutor requestExecutor = await CreateSchemaAsync(CodesAndGuidelinesMockFixture.codeStandardGuidelines, _cacheExecutor);  
        .......  
        .......    
    }  
}  

Below is the SchemaCache class

public class SchemaCache : QueryTestBase  
{  
    public SchemaCache(PostgreSqlResource resouce) : base(resouce) { }  
    public Task<IRequestExecutor> CreateSchemaAsync<T>(T[] entities,  
                                                       Dictionary<(Type, object), Task<IRequestExecutor>> cacheExecutor) where T : class  
    {  
        (Type, T[] entites) key = (typeof(T), entities);  
        if (cacheExecutor.TryGetValue(key, out var result))  
        {  
            return result;  
        }  
        else // else block will always be executed when I run the test methods all at once  
        {  
            cacheExecutor[key] = CreateDb(entities);  
        }  
        return cacheExecutor[key];  
    }  
}  

In the above class, I store the test data and type combined as keys in the dictionary and the results Task<IRequestExecutor> as values.

When executing the test methods all at once, For the first test method, it goes to the createDb, and the keys and values are stored in _cacheExecutor, and for the following test method _cacheExecutor has null values and thus it's going into the else block. The else block will always be executed because of the dictionary at class-level declaration, and for the new method, the _cacheExecutor will be null.

Could anyone please let me know any better solution or suggestion?

Thanks in advance!!

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

Developer technologies | ASP.NET Core | Other
0 comments No comments

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,606 Reputation points Volunteer Moderator
    2022-06-14T23:18:04.577+00:00

    No matter the test framework it's generally acceptable to perform a setup for each test method. For example, NUnit is [SetUp] public void Init() and teardown [TearDown] public void Cleanup() and MTest [TestInitialize] and [TestCleanup] to name a few. This way a test method is not polluted by other test methods.

    Perhaps that will help.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.