3

I am trying to create a test for a situation where an Update request throws an exception. Is this possible to do using FakeXRMEasy? I have tried using AddFakeMessageExecutor, but at the moment it is not working:

My fake message executor class:

public class UpdateExecutor : IFakeMessageExecutor
{
    public bool CanExecute(OrganizationRequest request)
    {
        return request is UpdateRequest;
    }

    public OrganizationResponse Execute(
        OrganizationRequest request,
        XrmFakedContext ctx)
    {
        throw new Exception();
    }

    public Type GetResponsibleRequestType()
    {
        return typeof(UpdateRequest);
    }
}

Use in test:

fakeContext.Initialize(new Entity[] { agreement });
fakeContext.AddFakeMessageExecutor<UpdateRequest>(new UpdateExecutor());

fakeContext.ExecuteCodeActivity<AgreementConfirmationWorkflow>(fakeContext.GetDefaultWorkflowContext());

And in the workflow the update request is called:

var workflowContext = executionContext.GetExtension<IWorkflowContext>();
var serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);

/// some code to retrieve entity and change attributes ///

service.Update(entity);

I wanted this to throw an exception, but at the moment the update request is completing successfully. How can I make this work?

André Cavaca
  • 480
  • 1
  • 5
  • 17
K. Oja
  • 63
  • 5

1 Answers1

3

IFakeMessageExecutor only works when you call IOrganizationService.Execute method. So, if you change your service.Update(entity); line of code for service.Execute(new UpdateRequest { Target = entity}); it should work.

Here is a full working example for reference:

CodeActivity

    public class RandomCodeActivity : CodeActivity
    {
        protected override void Execute(CodeActivityContext context)
        {
            var workflowContext = context.GetExtension<IWorkflowContext>();
            var serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
            var service = serviceFactory.CreateOrganizationService(workflowContext.UserId);

            var accountToUpdate = new Account() { Id = new Guid("e7efd527-fd12-48d2-9eae-875a61316639"), Name = "A new faked name!" };

            service.Execute(new UpdateRequest { Target = accountToUpdate });
        }
    }

FakeMessageExecutor instance

    public class FakeUpdateRequestExecutor : IFakeMessageExecutor
    {
        public bool CanExecute(OrganizationRequest request)
        {
            return request is UpdateRequest;
        }

        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
        {
            throw new InvalidPluginExecutionException("Throwing an Invalid Plugin Execution Exception for test purposes");
        }

        public Type GetResponsibleRequestType()
        {
            return typeof(UpdateRequest);
        }
    }

Test (uses xUnit test lib)

    [Fact]
    public void UpdateAccount_WithUpdateExecutorThrowingAnException_ExceptionThrown()
    {
        //Assign
        var context = new XrmFakedContext
        {
            ProxyTypesAssembly = Assembly.GetAssembly(typeof(Account))
        };

        var account = new Account() { Id = new Guid("e7efd527-fd12-48d2-9eae-875a61316639"), Name = "Faked Name" };

        context.Initialize(new List<Entity>() { account });
        context.AddFakeMessageExecutor<UpdateRequest>(new FakeUpdateRequestExecutor());
        var service = context.GetOrganizationService();

        //Act


        //Assert
        Assert.Throws<InvalidPluginExecutionException>(() => context.ExecuteCodeActivity<RandomCodeActivity>(account));
    }
André Cavaca
  • 480
  • 1
  • 5
  • 17
  • Thank you, this does work. Though a bit inconvenient having to use the more wordy syntax in the actual code. – K. Oja Apr 08 '19 at 20:54