你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

ActionMock 类

命名空间:Microsoft.Azure.Workflows.UnitTesting.Definitions

此类为标准逻辑应用工作流中的作创建模拟实例。 此类 *ActionMock 提供了多种方法来创建模拟作,以便基于执行上下文使用静态输出、错误条件或动态行为测试标准工作流。

用法

// Simple mock action with success status
var successAction = new ActionMock(TestWorkflowStatus.Succeeded, "SendEmail");

// A mock action with specific outputs
var outputAction = new ActionMock(
    TestWorkflowStatus.Succeeded, 
    "HttpRequest",
    new MockOutput { 
        StatusCode = 200,
        Headers = JToken.Parse(@"{""Content-Type"": ""application/json""}"),
        Body = JToken.Parse(@"{""result"": ""success"", ""id"": 12345}")
    });

// Failed action with error information
var failedAction = new ActionMock(
    TestWorkflowStatus.Failed,
    "DatabaseWrite",
    new TestErrorInfo(
        ErrorResponseCode.BadRequest,
        "Database connection failed"
    ));

// Dynamic action that changes behavior based on execution context
var dynamicAction = new ActionMock(
    (context) => {
        var inputs = context.ActionContext.ActionInputs;
        var amount = (int)inputs["amount"];
        
        if (amount > 1000) {
            return new ActionMock(TestWorkflowStatus.Failed, "PaymentProcessing", 
                new TestErrorInfo(ErrorResponseCode.BadRequest, "Amount exceeds limit"));
        }
        
        return new ActionMock(TestWorkflowStatus.Succeeded, "PaymentProcessing",
            new MockOutput { Body = JToken.Parse(@"{""transactionId"": ""ABC123""}") });
    },
    "DynamicPaymentAction");

构造函数

具有静态输出的构造函数

使用静态输出创建模拟实例 ActionMock

public ActionMock(TestWorkflowStatus status, string name = null, MockOutput outputs = null)
名称 DESCRIPTION 类型 必选
地位 模拟作结果状态。 TestWorkflowStatus 是的
姓名 模拟作名称。 字符串
输出 模拟静态输出。 MockOutput
// Example: Create a mock action with successful status and static outputs
var outputs = new MockOutput { 
    Body = JToken.Parse(@"{""result"": ""Operation completed""}"),
    StatusCode = 200
};
var actionMock = new ActionMock(TestWorkflowStatus.Succeeded, "ProcessData", outputs);

包含错误信息的构造函数

创建具有静态错误信息的模拟实例 ActionMock

public ActionMock(TestWorkflowStatus status, string name = null, TestErrorInfo error = null)
名称 DESCRIPTION 类型 必选
地位 模拟作结果状态。 TestWorkflowStatus 是的
姓名 模拟作名称。 字符串
错误 模拟作错误信息。 TestErrorInfo
// Example: Create an action mock with failed status and error information
var errorInfo = new TestErrorInfo(
    ErrorResponseCode.InternalServerError,
    "Service temporarily unavailable"
);
var actionMock = new ActionMock(TestWorkflowStatus.Failed, "ExternalAPICall", errorInfo);

具有回调函数的构造函数

使用动态输出的回调函数创建模拟实例 ActionMock

public ActionMock(Func<TestExecutionContext, ActionMock> onGetActionMock, string name = null)
名称 DESCRIPTION 类型 必选
onGetActionMock 用于获取模拟作的回调函数 Func<TestExecutionContextActionMock> 是的
姓名 模拟作名称 字符串
// Example: Create a mock action with dynamic outputs based on execution context
var actionMock = new ActionMock(
    (context) => {
        var actionName = context.ActionContext.ActionName;
        var inputs = context.ActionContext.ActionInputs;
          // Determine outputs dynamically based on context
        if (actionName == "ValidateUser" && inputs["userId"]?.Value<int>() > 0) {
            return new ActionMock(
                TestWorkflowStatus.Succeeded,
                "ValidateUser", 
                new MockOutput { Body = JToken.Parse(@"{""isValid"": true}") }
            );
        }
        
        return new ActionMock(TestWorkflowStatus.Failed, "ValidateUser");
    }, 
    "ConditionalValidation");

JSON 构造函数

从 JSON 创建模拟实例 ActionMock

internal ActionMock(TestWorkflowStatus status, string name = null, JToken outputs = null, TestErrorInfo error = null)
名称 DESCRIPTION 类型 必选
地位 模拟作结果状态。 TestWorkflowStatus 是的
姓名 模拟作名称。 字符串
输出 模拟输出。 MockOutput
错误 模拟错误。 TestErrorInfo
// Example: Create a mock action from JSON
var actionFromJson = JsonConvert.DeserializeObject<ActionMock>(File.ReadAllText(mockDataPath));

性能

此类从 OperationMock 基类继承以下属性。

名称 DESCRIPTION 类型 必选
名称 获取或设置模拟作的名称。 字符串
状态 获取或设置作状态。 TestWorkflowStatus
输出 获取或设置一个值,该值表示 JSON 格式的静态输出。 JToken
错误 获取或设置作错误。 TestErrorInfo