jordimontana82 / fake-xrm-easy

The testing framework for Dynamics CRM and Dynamics 365 which runs on an In-Memory context and deals with mocks or fakes for you
https://dynamicsvalue.com/get-started/overview?source=git
Other
263 stars 182 forks source link

PipelineSimulation ignores steps registered in PreValidation #500

Open BetimBeja opened 4 years ago

BetimBeja commented 4 years ago

Hello Jordi, steps registered in PreValidation do not get called when PipelineSimulation property is true. The following test should fail in my case because ContactPlugins.Update.PreValidation.EnsureIsNotPrimary plugin needs a PreImage that I have not registered in the pipeline, but it doesn't fail, because the two plugins registered in PreValidation are not being fired.

using FakeXrmEasy;
using Models;
using System;
using System.Linq;
using Xunit;

namespace AccountPluginsTests.Create.Pipeline
{
    public class CreateTests
    {
        [Fact]
        public void Creating_account_updates_primary_contacts_parentcustomerid()
        {
            var context = new XrmFakedContext()
            {
                UsePipelineSimulation = true
            };
            var contact = new Contact()
            {
                Id = Guid.NewGuid()
            };
            var account = new Account()
            {
                PrimaryContactId = contact.ToEntityReference()
            };
            context.EnableProxyTypes(typeof(Contact).Assembly);
            context.EnableProxyTypes(typeof(SdkMessageProcessingStep).Assembly);
            context.Initialize(contact);
            context.RegisterPluginStep<AccountPlugins.Create.PreValidation.EnsureHasContact>("Create", ProcessingStepStage.Prevalidation);
            context.RegisterPluginStep<AccountPlugins.Create.PreOperation.EnsureHasContact>("Create", ProcessingStepStage.Preoperation);
            context.RegisterPluginStep<ContactPlugins.Update.PreValidation.EnsureIsNotPrimary>("Update", ProcessingStepStage.Prevalidation);

            var accountId = context.GetOrganizationService().Create(account);
            var updatedContact = context.CreateQuery<Contact>().FirstOrDefault(c => c.Id == contact.Id);

            Assert.NotEqual(contact.ParentCustomerId, updatedContact.ParentCustomerId);
            Assert.NotNull(updatedContact.ParentCustomerId);
            Assert.Equal(accountId, updatedContact.ParentCustomerId.Id);
        }
    }
}
BetimBeja commented 4 years ago

Also, the ExecutePipelineStage used for Delete, should be implemented for other messages where the context is an EntityReference too. And for custom Actions, maybe we have to create a special FakeMessageExecutor.

jordimontana82 commented 4 years ago

Thanks Betim, yeah, that has changed in v2.x. It will be possible to register plugins for basically any message and it'll be much easier to extend ;)