Wednesday, May 7, 2025

Act Arrange 2

 using FakeXrmEasy;

using FakeItEasy;

using Microsoft.Xrm.Sdk;

using Xunit;


public class CaspPluginTests

{

    [Fact]

    public void Should_Call_CaspService_And_Set_Attribute()

    {

        // Arrange

        var context = new XrmFakedContext();

        var fakeService = A.Fake<ICaspService>();


        // Fake the method return value

        A.CallTo(() => fakeService.GetCaspMedicalData("123", "DLN456"))

            .Returns("{\"status\":\"success\"}");


        // Register your plugin and pass in the fake dependency

        var pluginContext = context.GetDefaultPluginContext();


        // Example: simulate a target entity

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

        target["pfx_contactid"] = "123";

        target["pfx_dln"] = "DLN456";


        pluginContext.InputParameters["Target"] = target;


        // Inject the fake dependency using constructor injection or a service locator

        var plugin = new CaspPlugin(fakeService); // You must design your plugin to accept it


        // Act

        context.ExecutePluginWith(plugin, pluginContext);


        // Assert — e.g. verify attribute set based on the fake result

        var updated = context.Data[target.LogicalName][target.Id];

        Assert.True(updated.Contains("pfx_result"));

        Assert.Equal("{\"status\":\"success\"}", updated["pfx_result"]);

    }

}


No comments:

Post a Comment