1

Microsoft Dynamics CRM 2015.

I test Asp.Net Core controller's action. When I create new Lead record some plugin generates new Guid for lead.new_master_id field (it's type is string). Therefore after creating I retrive the record to get it's generated new_master_id value. How can I emulate this plugin behaviour through Fake Xrm Easy?

var fakedContext = new XrmFakedContext();
fakedContext.ProxyTypesAssembly = typeof(Lead).Assembly;
var entities = new Entity[]
{
  // is empty array
};

fakedContext.Initialize(entities);
var orgService = fakedContext.GetOrganizationService();

var lead = new Lead { FirstName = "James", LastName = "Bond" };
var leadId = orgService.Create(lead);

var masterId = orgService.Retrieve(Lead.EntityLogicalName, leadId, 
    new Microsoft.Xrm.Sdk.Query.ColumnSet(Lead.Fields.new_master_id))
    .ToEntity<Lead>().new_master_id;
Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182

1 Answers1

1

In v1.x of FakeXrmEasy you'll need to enable PipelineSimulation and register the plugin steps you would like to be fired on Create manually by registering their steps.

fakedContext.UsePipelineSimulation = true;

Once enabled, you'll need to enable the necessary steps via calling RegisterPluginStep. In your example you'll need to at least register something along the lines of:

fakedContext.RegisterPluginStep<LeadPlugin>("Create", ProcessingStepStage.Preoperation);

Where LeadPlugin would be the name of your plugin that generates the new_master_id property.

Keep in mind v1.x is limited in that it supports pipeline simulation for basic CRUD requests only.

Later versions (2.x and/or 3.x) come with a brand new middleware implementation allowing registering plugin steps for any message. Soon we'll be implementing automatic registration of plugin steps based on an actual environment and/or custom attributes.

Here's an example using the new middleware

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();
}

}

You can find more info on the QuickStart guide here

Disclaimer: I'm the author of FakeXrmEasy :)

Jordi
  • 1,460
  • 9
  • 9