# Verify.EntityFramework
[![Build status](https://ci.appveyor.com/api/projects/status/g6njwv0aox62atu0?svg=true)](https://ci.appveyor.com/project/SimonCropp/verify-entityframework)
[![NuGet Status](https://img.shields.io/nuget/v/Verify.EntityFramework.svg)](https://www.nuget.org/packages/Verify.EntityFramework/)
Extends [Verify](https://github.com/SimonCropp/Verify) to allow verification of EntityFramework bits.
## Contents
* [Usage](#usage)
* [ChangeTracking](#changetracking)
* [Queryable](#queryable)
## NuGet package
https://nuget.org/packages/Verify.EntityFramework/
## Usage
Enable VerifyEntityFramewok once at assembly load time:
```cs
VerifyEntityFramework.Enable();
```
snippet source | anchor
### ChangeTracking
Added, deleted, and Modified entities can be verified by performing changes on a DbContext and then verifying that context. This approach leverages the [EntityFramework ChangeTracker](https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.changetracking.changetracker).
#### Added entity
This test:
```cs
[Fact]
public async Task Added()
{
var options = DbContextOptions();
await using var context = new SampleDbContext(options);
context.Add(new Company {Content = "before"});
await Verify(context);
}
```
snippet source | anchor
Will result in the following verified file:
```txt
{
Added: {
Company: {
Id: 0,
Content: 'before'
}
}
}
```
snippet source | anchor
#### Deleted entity
This test:
```cs
[Fact]
public async Task Deleted()
{
var options = DbContextOptions();
await using (var context = new SampleDbContext(options))
{
context.Add(new Company {Content = "before"});
context.SaveChanges();
}
await using (var context = new SampleDbContext(options))
{
var company = context.Companies.Single();
context.Companies.Remove(company);
await Verify(context);
}
}
```
snippet source | anchor
Will result in the following verified file:
```txt
{
Deleted: {
Company: {
Id: 0
}
}
}
```
snippet source | anchor
#### Modified entity
This test:
```cs
[Fact]
public async Task Modified()
{
var options = DbContextOptions();
await using var context = new SampleDbContext(options);
var company = new Company {Content = "before"};
context.Add(company);
context.SaveChanges();
context.Companies.Single().Content = "after";
await Verify(context);
}
```
snippet source | anchor
Will result in the following verified file:
```txt
{
Modified: {
Company: {
Id: 0,
Content: {
Original: 'before',
Current: 'after'
}
}
}
}
```
snippet source | anchor
### Queryable
This test:
```cs
[Fact]
public async Task Queryable()
{
var database = await DbContextBuilder.GetDatabase("Queryable");
var dbContext = database.Context;
var queryable = dbContext.Companies.Where(x => x.Content == "value");
await Verify(queryable);
}
```
snippet source | anchor
Will result in the following verified file:
```txt
SELECT [c].[Id], [c].[Content]
FROM [Companies] AS [c]
WHERE [c].[Content] = N'value'
```
snippet source | anchor
## Icon
[Database](https://thenounproject.com/term/database/310841/) designed by [Creative Stall](https://thenounproject.com/creativestall/) from [The Noun Project](https://thenounproject.com/creativepriyanka).