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

Fakecontext isn't updating after target change plugin executed #567

Closed suodbons closed 3 years ago

suodbons commented 3 years ago

If plugin is like ConfigurationPlugin and after executing I try to retrieve target from context (not use the target object in inputs), I will get unchanged entity as if plugin didn't execute.

    public class ConfigurationPlugin : IPlugin
    {
        ...
        public void Execute(IServiceProvider serviceProvider)
        {
            var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            var target = (Entity)context.InputParameters["Target"];

            target["unsecure"] = _unsecureConfiguration;
            target["secure"] = _secureConfiguration;
        }
    }

Target object check in inputs works correctly. But if tests have one context and need info from other tests it could be uncomfortable.

        public void When_A_Plugin_Is_Executed_Configurations_Can_Be_Used()
        {
            var fakedContext = new XrmFakedContext();

            var guid1 = Guid.NewGuid();
            var target = new Entity("contact") { Id = guid1 };

            var inputParams = new ParameterCollection { new KeyValuePair<string, object>("Target", target) };

            var unsecureConfiguration = "Unsecure Configuration";
            var secureConfiguration = "Secure Configuration";

            //Execute our plugin against the selected target
            var plugCtx = fakedContext.GetDefaultPluginContext();
            plugCtx.InputParameters = inputParams;

            fakedContext.ExecutePluginWithConfigurations<ConfigurationPlugin>(plugCtx, unsecureConfiguration, secureConfiguration);

            // It will work
            Assert.True(target.Contains("unsecure"));
            Assert.True(target.Contains("secure"));
            Assert.Equal((string)target["unsecure"], unsecureConfiguration);
            Assert.Equal((string)target["secure"], secureConfiguration);

            // It will not work
            var targetFromContext = fakedContext.GetOrganizationService().Retrieve("contact", target.Id, new ColumnSet("unsecure", "secure"));
            Assert.True(targetFromContext.Contains("unsecure"));
            Assert.True(targetFromContext.Contains("secure"));
            Assert.Equal((string)targetFromContext["unsecure"], unsecureConfiguration);
            Assert.Equal((string)targetFromContext["secure"], secureConfiguration);
        }

Using FakeXrmEasy.9 v1.57.1

jordimontana82 commented 3 years ago

This is by design. Changes that are not persisted via explicitly calling methods like .Create, .Update etc won't be persisted and so can't be retrieved. The way I test changes made to the target is by checking the original object references, as you did above in the first example.