首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FakeXRMEasy:使用AddFakeMessageExecutor重写请求行为

FakeXRMEasy:使用AddFakeMessageExecutor重写请求行为
EN

Stack Overflow用户
提问于 2019-04-04 21:12:59
回答 1查看 1.5K关注 0票数 4

我试图为更新请求抛出异常的情况创建一个测试。这可以使用FakeXRMEasy吗?我尝试过使用AddFakeMessageExecutor,但目前它无法工作:

我的假消息执行者类:

代码语言:javascript
复制
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);
    }
}

在试验中使用:

代码语言:javascript
复制
fakeContext.Initialize(new Entity[] { agreement });
fakeContext.AddFakeMessageExecutor<UpdateRequest>(new UpdateExecutor());

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

在工作流中,更新请求被称为:

代码语言:javascript
复制
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);

我希望这会抛出一个异常,但目前更新请求正在成功完成。我怎么才能把这事做好?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-08 18:36:07

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

下面是一个完整的工作示例,供参考:

CodeActivity

代码语言:javascript
复制
    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实例

代码语言:javascript
复制
    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 (使用xUnit测试库)

代码语言:javascript
复制
    [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));
    }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55524986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档