From ccc2092c8f01f38e15082530aa05aefb3111e988 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Mon, 20 Oct 2025 21:34:14 +0200 Subject: [PATCH] Refactor `SwitchTests`: simplify syntax, replace `ActivityTestHelper` usage with direct `ExecuteAsync` method, and clean up unused imports. --- .../Branching/SwitchTests.cs | 79 ++++++++----------- 1 file changed, 34 insertions(+), 45 deletions(-) diff --git a/test/unit/Elsa.Activities.UnitTests/Branching/SwitchTests.cs b/test/unit/Elsa.Activities.UnitTests/Branching/SwitchTests.cs index 6d728866d..1709efd33 100644 --- a/test/unit/Elsa.Activities.UnitTests/Branching/SwitchTests.cs +++ b/test/unit/Elsa.Activities.UnitTests/Branching/SwitchTests.cs @@ -1,5 +1,5 @@ -using Elsa.Activities.UnitTests.Helpers; using Elsa.Expressions.Models; +using Elsa.Testing.Shared; using Elsa.Workflows; using NSubstitute; @@ -14,8 +14,7 @@ public class SwitchTests var switchActivity = new Switch(); // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -35,8 +34,7 @@ public class SwitchTests }; // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -47,7 +45,7 @@ public class SwitchTests // Arrange var switchActivity = new Switch { - Mode = new Input(SwitchMode.MatchFirst), + Mode = new(SwitchMode.MatchFirst), Cases = new List { new("Case 1", Expression.LiteralExpression(false), Substitute.For()), @@ -57,8 +55,7 @@ public class SwitchTests }; // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -69,7 +66,7 @@ public class SwitchTests // Arrange var switchActivity = new Switch { - Mode = new Input(SwitchMode.MatchFirst), + Mode = new(SwitchMode.MatchFirst), Cases = new List { new("False Case", Expression.LiteralExpression(false), Substitute.For()), @@ -78,8 +75,7 @@ public class SwitchTests }; // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -90,7 +86,7 @@ public class SwitchTests // Arrange var switchActivity = new Switch { - Mode = new Input(SwitchMode.MatchFirst), + Mode = new(SwitchMode.MatchFirst), Cases = new List { new("First True", Expression.LiteralExpression(true), Substitute.For()), @@ -99,8 +95,7 @@ public class SwitchTests }; // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -111,7 +106,7 @@ public class SwitchTests // Arrange var switchActivity = new Switch { - Mode = new Input(SwitchMode.MatchAny), + Mode = new(SwitchMode.MatchAny), Cases = new List { new("First True", Expression.LiteralExpression(true), Substitute.For()), @@ -121,8 +116,7 @@ public class SwitchTests }; // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -133,7 +127,7 @@ public class SwitchTests // Arrange var switchActivity = new Switch { - Mode = new Input(SwitchMode.MatchAny), + Mode = new(SwitchMode.MatchAny), Cases = new List { new("Case 1", Expression.LiteralExpression(false), Substitute.For()), @@ -143,8 +137,7 @@ public class SwitchTests }; // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -163,8 +156,7 @@ public class SwitchTests }; // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -180,8 +172,7 @@ public class SwitchTests }; // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -197,8 +188,7 @@ public class SwitchTests }; // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -209,7 +199,7 @@ public class SwitchTests // Arrange var switchActivity = new Switch { - Mode = new Input(SwitchMode.MatchFirst), + Mode = new(SwitchMode.MatchFirst), Cases = new List { new("First", Expression.LiteralExpression(false), Substitute.For()), @@ -219,8 +209,7 @@ public class SwitchTests }; // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -231,7 +220,7 @@ public class SwitchTests // Arrange var switchActivity = new Switch { - Mode = new Input(SwitchMode.MatchAny), + Mode = new(SwitchMode.MatchAny), Cases = new List { new("First", Expression.LiteralExpression(true), Substitute.For()), @@ -241,8 +230,7 @@ public class SwitchTests }; // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -262,8 +250,7 @@ public class SwitchTests }; // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -276,14 +263,13 @@ public class SwitchTests { Cases = new List { - new("Null condition", Expression.LiteralExpression((bool?)null), Substitute.For()) + new("Null condition", Expression.LiteralExpression(null), Substitute.For()) }, Default = Substitute.For() }; // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -301,8 +287,7 @@ public class SwitchTests }; // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -318,12 +303,11 @@ public class SwitchTests Assert.Empty(switchActivity.Cases); // Test that we can add cases - switchActivity.Cases.Add(new SwitchCase("Test", Expression.LiteralExpression(true), Substitute.For())); + switchActivity.Cases.Add(new("Test", Expression.LiteralExpression(true), Substitute.For())); Assert.Single(switchActivity.Cases); // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } @@ -338,12 +322,17 @@ public class SwitchTests Assert.NotNull(switchActivity.Mode); // Test setting different modes - switchActivity.Mode = new Input(SwitchMode.MatchAny); + switchActivity.Mode = new(SwitchMode.MatchAny); // Act & Assert - Should not throw any exceptions - var exception = await Record.ExceptionAsync(async () => - await ActivityTestHelper.ExecuteActivityAsync(switchActivity)); + var exception = await Record.ExceptionAsync(() => ExecuteAsync(switchActivity)); Assert.Null(exception); } + + private static Task ExecuteAsync(IActivity activity) + { + return new ActivityTestFixture(activity).ExecuteAsync(); + } + }