Click or drag to resize
ErrorUnit.JsonSerializer Namespace
The ErrorUnit.JsonSerializer namespace contains classes that ErrorUnit uses for JSON serialization.
Classes
  ClassDescription
Public classCode exampleDbContextSerializer

The ErrorUnit DbContextSerializer works best with a code first context however it is possible to use a model first implementation.

The context class needs to persist for the whole lifetime of the call, so the easiest way is to use a singleton pattern backed by System.Web.HttpContext.Current.Items and a static property for when it is used by Unit Testing. Additionally ErrorUnit needs a way to override the connection so we add a additional constructor.

Examples
So for a DbContext named Northwind the code that would be changed/added is:
using System;
using System.Data.Entity;

public partial class Northwind : DbContext
{
   [Obsolete("Use static Instance property instead")]
   public Northwind()
       : base("name=Northwind")
   {
   }

   private static Northwind _Instance;
#pragma  warning disable CS0618 // Type or member is obsolete
   public static Northwind Instance
   {
       get
       {
           return System.Web.HttpContext.Current != null
               ? (Northwind)(System.Web.HttpContext.Current.Items[typeof(Northwind).FullName] ?? (System.Web.HttpContext.Current.Items[typeof(Northwind).FullName] = new Northwind()))
               : _Instance ?? (_Instance = new Northwind());
       }
   }
#pragma  warning restore CS0618 // Type or member is obsolete

   [Obsolete("ErrorUnit uses this to build fake databases")]
   public Northwind(System.Data.Common.DbConnection existingConnection, bool contextOwnsConnection) : base(existingConnection, contextOwnsConnection) { }

    //Remaining Context Code...

And everywhere in code you would change new Northwind() to Northwind.Instance; and not use Northwind.Instance inside using statements

Note: If you are using a model first EF context you will have to create and keep up to date a SQL file that builds the database (aka a DDL); the DDL sql file must be created for SQL Compact Edition in the ErrorUnitTests directory of your Unit Test project and named after your context class name (in this example it would be named Northwind.sql). A DDL sql file can be created via this method https://msdn.microsoft.com/en-us/library/dd456815(v=vs.100).aspx and may need some adjusting to get it to work right.

Public classErrorUnitJsonSerializer
The Json serializer for ErrorUnit