14 KiB
Verify.EntityFramework
Extends Verify to allow snapshot testing with EntityFramework.
Part of the .NET Foundation
NuGet package
- https://nuget.org/packages/Verify.EntityFramework/
- https://nuget.org/packages/Verify.EntityFrameworkClassic/
Enable
Enable VerifyEntityFramework once at assembly load time:
EF Core
VerifyEntityFramework.Enable();
EF Classic
VerifyEntityFrameworkClassic.Enable();
Recording
Recording allows all commands executed by EF to be captured and then (optionally) verified.
Enable
Call SqlRecording.EnableRecording()
on DbContextOptionsBuilder
.
DbContextOptionsBuilder<SampleDbContext> builder = new();
builder.UseSqlServer(connection);
builder.EnableRecording();
SampleDbContext data = new(builder.Options);
EnableRecording
should only be called in the test context.
Usage
To start recording call SqlRecording.StartRecording()
. The results will be automatically included in verified file.
Company company = new()
{
Content = "Title"
};
data.Add(company);
await data.SaveChangesAsync();
SqlRecording.StartRecording();
await data.Companies
.Where(x => x.Content == "Title")
.ToListAsync();
await Verifier.Verify(data.Companies.Count());
Will result in the following verified file:
{
target: 5,
sql: [
{
Type: ReaderExecutedAsync,
Text:
SELECT [c].[Id], [c].[Content]
FROM [Companies] AS [c]
WHERE [c].[Content] = N'Title'
},
{
Type: ReaderExecuted,
Text:
SELECT COUNT(*)
FROM [Companies] AS [c]
}
]
}
Sql entries can be explicitly read using SqlRecording.FinishRecording
, optionally filtered, and passed to Verify:
Company company = new()
{
Content = "Title"
};
data.Add(company);
await data.SaveChangesAsync();
SqlRecording.StartRecording();
await data.Companies
.Where(x => x.Content == "Title")
.ToListAsync();
var entries = SqlRecording.FinishRecording();
//TODO: optionally filter the results
await Verifier.Verify(new
{
target = data.Companies.Count(),
sql = entries
});
DbContext spanning
StartRecording
can be called on different DbContext instances (built from the same options) and the results will be aggregated.
DbContextOptionsBuilder<SampleDbContext> builder = new();
builder.UseSqlServer(connectionString);
builder.EnableRecording();
await using SampleDbContext data1 = new(builder.Options);
SqlRecording.StartRecording();
Company company = new()
{
Content = "Title"
};
data1.Add(company);
await data1.SaveChangesAsync();
await using SampleDbContext data2 = new(builder.Options);
await data2.Companies
.Where(x => x.Content == "Title")
.ToListAsync();
await Verifier.Verify(data2.Companies.Count());
{
target: 5,
sql: [
{
Type: ReaderExecutedAsync,
HasTransaction: true,
Parameters: {
@p0 (Int32): 0,
@p1 (String?): Title
},
Text:
SET NOCOUNT ON;
INSERT INTO [Companies] ([Id], [Content])
VALUES (@p0, @p1);
},
{
Type: ReaderExecutedAsync,
Text:
SELECT [c].[Id], [c].[Content]
FROM [Companies] AS [c]
WHERE [c].[Content] = N'Title'
},
{
Type: ReaderExecuted,
Text:
SELECT COUNT(*)
FROM [Companies] AS [c]
}
]
}
ChangeTracking
Added, deleted, and Modified entities can be verified by performing changes on a DbContext and then verifying the instance of ChangeTracking. This approach leverages the EntityFramework ChangeTracker.
Added entity
This test:
[Test]
public async Task Added()
{
var options = DbContextOptions();
await using SampleDbContext data = new(options);
Company company = new()
{
Content = "before"
};
data.Add(company);
await Verifier.Verify(data.ChangeTracker);
}
Will result in the following verified file:
{
Added: {
Company: {
Id: 0,
Content: before
}
}
}
Deleted entity
This test:
[Test]
public async Task Deleted()
{
var options = DbContextOptions();
await using SampleDbContext data = new(options);
data.Add(new Company {Content = "before"});
await data.SaveChangesAsync();
var company = data.Companies.Single();
data.Companies.Remove(company);
await Verifier.Verify(data.ChangeTracker);
}
Will result in the following verified file:
{
Deleted: {
Company: {
Id: 0
}
}
}
Modified entity
This test:
[Test]
public async Task Modified()
{
var options = DbContextOptions();
await using SampleDbContext data = new(options);
Company company = new()
{
Content = "before"
};
data.Add(company);
await data.SaveChangesAsync();
data.Companies.Single().Content = "after";
await Verifier.Verify(data.ChangeTracker);
}
Will result in the following verified file:
{
Modified: {
Company: {
Id: 0,
Content: {
Original: before,
Current: after
}
}
}
}
Queryable
This test:
var queryable = data.Companies
.Where(x => x.Content == "value");
await Verifier.Verify(queryable);
Will result in the following verified file:
EF Core
SELECT [c].[Id], [c].[Content]
FROM [Companies] AS [c]
WHERE [c].[Content] = N'value'
EF Classic
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Content] AS [Content]
FROM [dbo].[Companies] AS [Extent1]
WHERE N'value' = [Extent1].[Content]
AllData
This test:
await Verifier.Verify(data.AllData())
.ModifySerialization(
serialization =>
serialization.AddExtraSettings(
serializer =>
serializer.TypeNameHandling = TypeNameHandling.Objects));
Will result in the following verified file with all data in the database:
[
{
$type: Company,
Id: Id_1,
Content: Company1
},
{
$type: Company,
Id: Id_2,
Content: Company2
},
{
$type: Company,
Id: Id_3,
Content: Company3
},
{
$type: Company,
Id: Id_4,
Content: Company4
},
{
$type: Employee,
Id: Id_5,
CompanyId: Id_1,
Content: Employee1,
Age: 25
},
{
$type: Employee,
Id: Id_6,
CompanyId: Id_1,
Content: Employee2,
Age: 31
},
{
$type: Employee,
Id: Id_7,
CompanyId: Id_2,
Content: Employee4,
Age: 34
}
]
IgnoreNavigationProperties
IgnoreNavigationProperties
extends SerializationSettings
to exclude all navigation properties from serialization:
[Test]
public async Task IgnoreNavigationProperties()
{
var options = DbContextOptions();
await using SampleDbContext data = new(options);
Company company = new()
{
Content = "company"
};
Employee employee = new()
{
Content = "employee",
Company = company
};
await Verifier.Verify(employee)
.ModifySerialization(
x => x.IgnoreNavigationProperties(data));
}
Icon
Database designed by Creative Stall from The Noun Project.