Skip to content

Latest commit

 

History

History

EF

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

nuget

Hypothesist.EF

Use Hypothesist to observe and validate entity events on an Entity Framework context.

Arrange

await using var context = new TestContext(...);
var observer = context.ObserverFor<Item>();

Act

context.Items.Add(new Item(0));
await context.SaveChangesAsync();

Assert

await Hypothesis
    .On(observer)
    .Timebox(2.Seconds())
    .Any()
    .Match(new Item(1))
    .Validate();

Filter

If you need the state change of the entity in your validation, specify the filter on the observer:

var observer = context.ObserverFor<Item>(EntityState.Added);

or:

var observer = context.ObserverFor<Item>(args => args.State == EntityState.Added);

Remark

Don't use this for normal straightforward in-process entity framework events where you can just use normal async/await operations. Use this as a validation entry-point for out-of-process interactions from a test with your system-under-test!

A great example is with dapr, where:

  1. your test invokes the sidecar
  2. the sidecar sends a message to a pub/sub broker
  3. the broker puts a request on your API endpoint
  4. the API endpoint invokes some business logic
  5. the business logic ultimately stores something in a database

Here you leverage the pluggable component of the database context to validate that the expected entity event occured. Another option is to hook into the middleware of ASP.NET, for example using the ASP.NET adapter.