C#C
C#3y ago
SWEETPONY

Is is possible to "mock" dbcontext?

I would like to write some unit tests for my services. For example, let's look at my method:
public EntityModification<SecuritySettings> MifareSecuritySettingsSet(
    SecuritySettings dto,
    CultureInfo culture )
{
    using var dbContext = DbContextFactory.CreateDbContext();
    var existedSettings = SettingsGet( dbContext );          
    SecuritySettings old = null;      

    existedSettings = new Settings                          
    {                                                       
         SecuritySettings = Clone( dto ),                    
         MifareSettings = new MifareModeSettings             
         {                                                   
            ReadMifarePlusMode = MifarePlusModeType.Disabled
         }                                                   
    };

     dbContext.SaveChanges();                                
                                                        
     return new EntityModification<SecuritySettings>         
     {                                                       
        Old = old,                                          
        Current = SettingsGet( dbContext )?.SecuritySettings
     };                                                                                                                                   
}


The problem is db context. I wanna test this method without real changes in my database so how can I do it?
Was this page helpful?