Freigeben über


ActionMock-Klasse

Namespace: Microsoft.Azure.Workflows.UnitTesting.Definitions

Diese Klasse erstellt eine Pseudoinstanz für eine Aktion in einem Standardlogik-App-Workflow. Die *ActionMock Klasse bietet mehrere Möglichkeiten, simulierte Aktionen zum Testen von Standardworkflows mit statischen Ausgaben, Fehlerbedingungen oder dynamischem Verhalten basierend auf dem Ausführungskontext zu erstellen.

Verwendung

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

Erbauer

Konstruktor mit statischen Ausgaben

Erstellt eine Pseudoinstanz für ActionMock statische Ausgaben.

public ActionMock(TestWorkflowStatus status, string name = null, MockOutput outputs = null)
Name BESCHREIBUNG Typ Erforderlich
Status Der Status des Pseudoaktionsergebnisses. TestWorkflowStatus Ja
Name Der Simulierte Aktionsname. Schnur Nein
Ergebnisse Die simulierten statischen Ausgaben. MockOutput Nein
// 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);

Konstruktor mit Fehlerinformationen

Erstellt eine Pseudoinstanz für ActionMock statische Fehlerinformationen.

public ActionMock(TestWorkflowStatus status, string name = null, TestErrorInfo error = null)
Name BESCHREIBUNG Typ Erforderlich
Status Der Status des Pseudoaktionsergebnisses. TestWorkflowStatus Ja
Name Der Simulierte Aktionsname. Schnur Nein
Fehler Die Fehlerinformationen für simulierte Aktionen. TestErrorInfo Nein
// 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);

Konstruktor mit Rückruffunktion

Erstellt eine Simulierte Instanz für ActionMock eine Rückruffunktion für dynamische Ausgaben.

public ActionMock(Func<TestExecutionContext, ActionMock> onGetActionMock, string name = null)
Name BESCHREIBUNG Typ Erforderlich
onGetActionMock Die Rückruffunktion zum Abrufen der simulierten Aktion Func<TestExecutionContext, ActionMock> Ja
Name Der Name der simulierten Aktion Schnur Nein
// 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-Konstruktor

Erstellt eine Pseudoinstanz für ActionMock JSON.

internal ActionMock(TestWorkflowStatus status, string name = null, JToken outputs = null, TestErrorInfo error = null)
Name BESCHREIBUNG Typ Erforderlich
Status Der Status des Pseudoaktionsergebnisses. TestWorkflowStatus Ja
Name Der Simulierte Aktionsname. Schnur Nein
Ergebnisse Die simulierten Ausgaben werden ausgegeben. MockOutput Nein
Fehler Der Simulierte Fehler. TestErrorInfo Nein
// Example: Create a mock action from JSON
var actionFromJson = JsonConvert.DeserializeObject<ActionMock>(File.ReadAllText(mockDataPath));

Eigenschaften

Diese Klasse erbt die folgenden Eigenschaften von der OperationMock Basisklasse.

Name BESCHREIBUNG Typ Erforderlich
Name Dient zum Abrufen oder Festlegen des Namens für den Pseudovorgang. Schnur Nein
Der Status Dient zum Abrufen oder Festlegen des Vorgangsstatus. TestWorkflowStatus Nein
Ausgaben Dient zum Abrufen oder Festlegen eines Werts, der die statische Ausgabe im JSON-Format darstellt. JToken Nein
Fehler Ruft den Vorgangsfehler ab oder legt den Vorgangsfehler fest. TestErrorInfo Nein