C
C#2y 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
};
}
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?
11 Replies
SWEETPONY
SWEETPONYOP2y ago
DbContextFactory is: protected readonly ISegmentDbContextFactory DbContextFactory;
public interface ISegmentDbContextFactory
: IDbContextFactory<SegmentDbContext>
{
}
public interface ISegmentDbContextFactory
: IDbContextFactory<SegmentDbContext>
{
}
public class SegmentDbContext : DbContext
{
public SegmentDbContext( DbContextOptions options ) : base( options )
{
}
}
public class SegmentDbContext : DbContext
{
public SegmentDbContext( DbContextOptions options ) : base( options )
{
}
}
amio
amio2y ago
I think it's more common to create a DbContext that is in-memory, populate it with any sensible test data etc
jcotton42
jcotton422y ago
I personally would test against a real database just point your db context towards a test db
amio
amio2y ago
That too.
SWEETPONY
SWEETPONYOP2y ago
test db hmm ok, thanks for advices I'll try it but in my opinion test db is a problem.. you always need to check data in db
jcotton42
jcotton422y ago
wdym "check data in db"?
SWEETPONY
SWEETPONYOP2y ago
you should always check if there is data in the database. If the database is not filled with data, the tests may not work, and not exactly what I want
amio
amio2y ago
Not if you're creating a blank one and equipping it properly for each test - scaffoldable.
jcotton42
jcotton422y ago
as the db is dedicated to testing, you your tests would just fill it with the needed data
SWEETPONY
SWEETPONYOP2y ago
smth like "fill database, test the method, remove what you filled before"? it looks like in-memory
jcotton42
jcotton422y ago
something like that the reason I wouldn't use in-memory is it may not act like the real database
Want results from more Discord servers?
Add your server