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:
The problem is db context. I wanna test this method without real changes in my database so how can I do it?
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
};
}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?