4

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

我的假消息执行器类:

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

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

4

1 回答 1

4

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

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

代码活动

    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));
    }
于 2019-04-08T18:36:07.460 回答