Testing Update messages with PreEntityImages

Oct 2, 2015 at 8:47 PM
How do I write a Unit Test with a defined PreImage? the base has a SetTarget method that sets the PluginExecutionContext.InputParameters, but I don't see any methods in the source to set the PluginExecutionContext.PreEntityImages. Please help!
Oct 4, 2015 at 1:38 PM
Edited Oct 4, 2015 at 1:41 PM
 protected override Microsoft.Xrm.Sdk.IPlugin SetupPlugin()
        {
            base.SetPluginEvent("annotation", "Update", Xrm.Framework.Test.Unit.SdkMessageProcessingStepImage.PostOperation);

           
            var annotation = new Entity();
            base.SetTarget(annotation);

         //This is what tjasonMayo asked for v. 3.1.0.2
            this.PluginExecutionContext.PostEntityImages.Add("PostImage", annotation);
            


IPlugin plugin = new PostUpdate();


            return plugin;
        }
Oct 5, 2015 at 3:26 PM

I appreciate the response, but when I add a new Entity to the PreEntityImages EntityImageCollection in SetupPlugin and debug the plugin code, the Context.PluginContext.PreEntityImages is empty…am I missing something? Did this code actually work for you?

Here is my SetupPlugin method:

protected override IPlugin SetupPlugin()

{

base.SetPluginEvent(Account.EntityLogicalName, Constants.Plugin.Message.Update, Xrm.Framework.Test.Unit.SdkMessageProcessingStepImage.PreOperation);

var account = new Entity(Account.EntityLogicalName);

base.SetTarget(account);

this.PluginExecutionContext.PreEntityImages.Add(Constants.Plugin.Target, account);

IPlugin plugin = new NameConcatenation();

return plugin;

}

This condition fails in the Plugin:

if (Context.PluginContext.InputParameters.Contains(Constants.Plugin.Target) && Context.PluginContext.InputParameters[Constants.Plugin.Target] is Entity)

Context.PluginContext.PreEntityImages.Count always equals zero! L

Oct 5, 2015 at 5:37 PM
Now I am the one that not understand ...? Here I speak for PreImages , PostImages - your code is showing InputParameters?
Oct 5, 2015 at 6:01 PM

Sorry, the plugin code I meant to paste was this:

if (Context.PluginContext.PreEntityImages.Contains(Model.Constants.Plugin.Target) && Context.PluginContext.PreEntityImages[Model.Constants.Plugin.Target] is Entity)

Like I stated, the Context.PluginContext.PreEntityImages collection is always empty (Count = 0).

Oct 5, 2015 at 6:04 PM
protected override Microsoft.Xrm.Sdk.IPlugin SetupPlugin()
    {
        base.SetPluginEvent("annotation", "Update", Xrm.Framework.Test.Unit.SdkMessageProcessingStepImage.PostOperation);


        var annotation = new Entity();

        base.SetTarget(annotation);
        this.PluginExecutionContext.PostEntityImages.Add("PostImage", annotation);
        IPlugin plugin = new PostUpdate();


        return plugin;
    }

this is works for me?
Oct 5, 2015 at 9:31 PM

The code you sent “works” in that it compiles and runs, but can you access the PostEntityImages collection in the plugin itself and see the annotation Entity? The PostEntityImages collection in the plugin’s context is empty!

Oct 6, 2015 at 4:42 PM

Any other suggestions? I can’t believe the xRM Test Framework wouldn’t support unit testing update plugins with pre and post images! Thanks.

Oct 6, 2015 at 6:47 PM

I figured it out! You can’t add an entity image to the base.PluginExecutionContext.PreEntityImages, but you can override the base.PluginExecutionContextStub.PreEntityImagesGet method as follows:

Account preImage = new Account();

EntityImageCollection preEntityImages = new EntityImageCollection();

preEntityImages.Add(Constants.Plugin.Target, preImage.ToEntity<Entity>());

base.PluginExecutionContextStub.PreEntityImagesGet = () =>

{

return preEntityImages;

};

Works like a charm. J