Middleware

FakeXrmEasy versions 2.x and 3.x come with a brand new middleware capable of executing a fully configurable pipeline.

In previous versions (1.x) there was one single implementation executed for every OrganizationRequest.

With the new architecture, every OrganizationRequest is executed through a pipeline of configurable steps (the middleware), making extending it way easier and also decoupling each extension package from other packages.

You would typically add the middleware setup in a base test class so it has to be defined only once for all the unit tests in your test project.

Minimal Setup

In this example, the middleware is using a middleware setup with support for CRUD operations only.

CRUD contains implementations for the following messages:

  • Retrieve
  • RetrieveMultiple
  • Create
  • Update
  • Delete
  • Upsert (if available depending on the Dataverse / CRM version)
  • Associate
  • Dissassociate
public class FakeXrmEasyTestsBase
{
    protected readonly IXrmFakedContext _context;
    protected readonly IOrganizationServiceAsync2 _service;

    public FakeXrmEasyTestsBase() 
    {
        _context = MiddlewareBuilder
                        .New()
                        .AddCrud()
                        
                        .UseCrud()
                        .Build();

        _service = _context.GetAsyncOrganizationService2();
    }
}

Extra Messages Setup

By adding AddFakeMessageExecutors we tell FakeXrmEasy to also search for other FakeXrmEasy message executors in the current assembly.

You can call this method with other assemblies as many times as you want, too.

If you don’t specify any parameters to the .AddFakeMessageExecutors() method, it’ll search for FakeMessageExecutors in the current assembly.

public class FakeXrmEasyTestsBase
{
    protected readonly IXrmFakedContext _context;
    protected readonly IOrganizationServiceAsync2 _service;

    public FakeXrmEasyTestsBase() 
    {
        _context = MiddlewareBuilder
                        .New()

                        .AddCrud()
                        .AddFakeMessageExecutors()

                        .UseCrud()
                        .UseMessages()
                        .Build();

        _service = _context.GetAsyncOrganizationService2();
    }
}

When importing the fake messages from the FakeXrmEasy.Messages NuGet package, you’ll need to explicitly tell FakeXrmEasy to search for fake messages in that assembly, and so the setup in that case would look like the following:

public class FakeXrmEasyTestsBase
{
    protected readonly IXrmFakedContext _context;
    protected readonly IOrganizationServiceAsync2 _service;

    public FakeXrmEasyTestsBase() 
    {
        _context = MiddlewareBuilder
                        .New()

                        .AddCrud()
                        .AddFakeMessageExecutors(Assembly.GetAssembly(typeof(AddListMembersListRequestExecutor)))

                        .UseCrud()
                        .UseMessages()
                        .Build();

        _service = _context.GetAsyncOrganizationService2();
    }
}

Generic Fake Message Executors Setup

There are some types of fake messages for which there is no associated strongly typed object generated in the CRM / Dataverse SDK. These can be messages like, for example, late-bound custom actions, or out of the box messages.

There is a specific construct for these in the pipeline .AddGenericFakeMessageExecutors():

public class FakeXrmEasyTestsBase
{
    protected readonly IXrmFakedContext _context;
    protected readonly IOrganizationServiceAsync2 _service;

    public FakeXrmEasyTestsBase() 
    {
        _context = MiddlewareBuilder
                        .New()

                        .AddCrud()
                        .AddGenericFakeMessageExecutors(Assembly.GetAssembly(typeof (NavigateToNextEntityOrganizationRequestExecutor)))


                        .UseCrud()
                        .UseMessages()
                        .Build();

        _service = _context.GetAsyncOrganizationService2();
    }
}

Pipeline Simulation Setup

Pipeline simulation setup that will fire plugin executions if you have registered the necessary plugin steps in your unit test setup.

public class FakeXrmEasyTestsBase
{
    protected readonly IXrmFakedContext _context;
    protected readonly IOrganizationServiceAsync2 _service;

    public FakeXrmEasyTestsBase() 
    {
        _context = MiddlewareBuilder
                        .New()

                        .AddCrud()
                        .AddFakeMessageExecutors()
                        .AddPipelineSimulation()

                        .UsePipelineSimulation()
                        .UseCrud()
                        .UseMessages()

                        .Build();

        _service = _context.GetAsyncOrganizationService2();
    }
}