Skip to content

spkl.fakes

Scott Durow edited this page Oct 15, 2017 · 5 revisions

spkl.fakes provides a simple way of mocking the Dynamics 365 Plugin/Workflow and Organization Service.

using (var pipeline = new PluginPipeline(
                FakeMessageNames.Create, 
                FakeStages.PreOperation, 
                new Entity("account")))
{
    var plugin = new AccountPlugin();
    try
    {
        pipeline.Execute(plugin);
    }
}

spkl.fakes is different to other unit test frameworks for Dynamics 365 in that it uses Microsoft fakes to create an emulator rather than trying to reimplement the whole of the Dynamics run time.

You provide the expected calls that your code will call and spkl.fakes will report if anything unexpected happens.

// Arrange
var plugin = new AccountPlugin();
pipeline.FakeService.ExpectRetrieve((entityName, id, columnSet) =>
{
    if (entityName == "account")
    {
        var account = new Entity(entityName)
        {
            Id = id
        };
        account["name"] = "Account 123";
        return account;
    }
    else
    {
        Assert.Fail("Unexpected call");
        return null;
    }

});

// Act
pipeline.Execute(plugin);

// Assert
pipeline.FakeService.AssertExpectedCalls();
Assert.AreEqual("Account 123", pipeline.Target.GetAttributeValue<string>("new_attribute"));

See https://github.com/scottdurow/SparkleXrm/blob/master/spkl.fakes/Microsoft.Crm.Sdk.Fakes.Tests/FakeOrganizationServiceTests.cs for examples of creating a IOrganizationService emulator.

Clone this wiki locally