Exemple #1
0
        public void Test_plugin_2011_preupdate_messagetype_execute()
        {
            //Arrange

            //Define the Target entity
            Entity target = new Entity("account")
            {
                Id = Guid.NewGuid()
            };
            //Create the plug-in mock and set the MessageName using a helper method, CreateMessageName.GetCustom
            //can be used to specify a message not included or for a custom action
            CrmPluginMock pluginMock = new CrmPluginMock
            {
                PluginExecutionContext = new FakePluginExecutionContext
                {
                    MessageName = XrmMoq.Helpers.CreateMessageName.GetExisting(MessageName.create)
                }
            };

            //Set the Target entity - there is an overload for EntityReference as well
            pluginMock.SetTarget(ref target);

            //Most of the specific OrganizationResponse classes do not have 'setters' on their properties so to get
            //around this create the specific response but instead define any return values in the 'Results' collection.
            //Define the mock Execute response
            OrganizationResponse response = new WhoAmIResponse
            {
                Results = new ParameterCollection
                {
                    { "UserId", new Guid("D1D92A0D-8BC8-4001-BE3E-A2C5D36E5124") }
                }
            };

            //Set the Execute response
            pluginMock.SetMockExecutes(response);

            //Act

            //Execute the plug-in
            pluginMock.Execute <ExamplePlugin>();

            //Assert

            //Check the value updated in the Target entity matches the expected value being
            //returned from the plug-in
            Assert.AreEqual(target.GetAttributeValue <string>("name").ToUpper(), "D1D92A0D-8BC8-4001-BE3E-A2C5D36E5124");
        }