Extends Verify to allow verification of EntityFramework bits.
Перейти к файлу
Simon Cropp c116880220 docs 2020-08-19 13:36:48 +10:00
.github . 2020-08-11 08:31:59 +10:00
src docs 2020-08-19 13:36:48 +10:00
.gitignore . 2020-01-06 15:58:10 +11:00
code_of_conduct.md . 2020-07-25 09:59:16 +10:00
license.txt Update license.txt 2020-07-25 08:59:20 +10:00
readme.md docs 2020-08-19 13:36:48 +10:00
readme.source.md validate ChangeTracker explcitly. not context (#204) 2020-08-19 13:32:40 +10:00

readme.md

Verify.EntityFramework

Build status NuGet Status NuGet Status

Extends Verify to allow verification of EntityFramework bits.

Support is available via a Tidelift Subscription.


Part of the .NET Foundation

Contents

NuGet package

Usage

Enable VerifyEntityFramewok once at assembly load time:

EF Core

VerifyEntityFramework.Enable();

snippet source | anchor

EF Classic

VerifyEntityFrameworkClassic.Enable();

snippet source | anchor

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 var data = new SampleDbContext(options);
    var company = new Company
    {
        Content = "before"
    };
    data.Add(company);
    await Verifier.Verify(data.ChangeTracker);
}

snippet source | anchor

Will result in the following verified file:

{
  Added: {
    Company: {
      Id: 0,
      Content: 'before'
    }
  }
}

snippet source | anchor

Deleted entity

This test:

[Test]
public async Task Deleted()
{
    var options = DbContextOptions();

    await using var data = new SampleDbContext(options);
    data.Add(new Company {Content = "before"});
    await data.SaveChangesAsync();

    var company = data.Companies.Single();
    data.Companies.Remove(company);
    await Verifier.Verify(data.ChangeTracker);
}

snippet source | anchor

Will result in the following verified file:

{
  Deleted: {
    Company: {
      Id: 0
    }
  }
}

snippet source | anchor

Modified entity

This test:

[Test]
public async Task Modified()
{
    var options = DbContextOptions();

    await using var data = new SampleDbContext(options);
    var company = new Company
    {
        Content = "before"
    };
    data.Add(company);
    await data.SaveChangesAsync();

    data.Companies.Single().Content = "after";
    await Verifier.Verify(data.ChangeTracker);
}

snippet source | anchor

Will result in the following verified file:

{
  Modified: {
    Company: {
      Id: 0,
      Content: {
        Original: 'before',
        Current: 'after'
      }
    }
  }
}

snippet source | anchor

Queryable

This test:

[Test]
public async Task Queryable()
{
    var database = await DbContextBuilder.GetDatabase("Queryable");
    var data = database.Context;
    var queryable = data.Companies
        .Where(x => x.Content == "value");
    await Verifier.Verify(queryable);
}

snippet source | anchor

Will result in the following verified file:

EF Core

SELECT [c].[Id], [c].[Content]
FROM [Companies] AS [c]
WHERE [c].[Content] = N'value'

snippet source | anchor

EF Classic

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Content] AS [Content]
    FROM [dbo].[Companies] AS [Extent1]
    WHERE N'value' = [Extent1].[Content]

snippet source | anchor

Security contact information

To report a security vulnerability, use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

Icon

Database designed by Creative Stall from The Noun Project.