FakeXRMEasy:使用 AddFakeMessageExecutor 覆盖更新请求行为

FakeXRMEasy: Using AddFakeMessageExecutor to override update request behaviour

我正在尝试针对更新请求引发异常的情况创建测试。使用 FakeXRMEasy 可以做到这一点吗?我试过使用 AddFakeMessageExecutor,但目前它不起作用:

我的假消息执行器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);
    }
}

在测试中使用:

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

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

并且在工作流中调用更新请求:

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);

我希望这会引发异常,但目前更新请求已成功完成。我怎样才能使这项工作?

IFakeMessageExecutor 仅在调用 IOrganizationService.Execute 方法时有效。因此,如果您将 service.Update(entity); 代码行更改为 service.Execute(new UpdateRequest { Target = entity});,它应该可以工作。

这里有一个完整的工作示例供参考:

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 实例

    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);
        }
    }

测试(使用 xUnit 测试库)

    [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));
    }