我试图为更新请求抛出异常的情况创建一个测试。这可以使用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);我希望这会抛出一个异常,但目前更新请求正在成功完成。我怎么才能把这事做好?
发布于 2019-04-08 18:36:07
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);
}
}test (使用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));
}https://stackoverflow.com/questions/55524986
复制相似问题