From 4ea883c44aaef8cba8207deb91f37c39259da39e Mon Sep 17 00:00:00 2001 From: Lilla Orban Date: Fri, 12 Apr 2024 22:28:53 +0200 Subject: [PATCH] fix(module-loader) - Using FluentAssertions in ModuleLoaderTests --- ...eUI.ModuleLoader.Abstractions.Tests.csproj | 1 + .../StartupContextTests.cs | 12 +++-- .../ServiceCollectionExtensionsTests.cs | 7 +-- .../ModuleInstanceTests.cs | 13 +++-- .../ModuleLoaderTests.cs | 48 +++++++++++-------- .../Runners/NativeModuleRunnerTests.cs | 20 ++++---- .../Runners/WebModuleRunnerTests.cs | 9 ++-- 7 files changed, 63 insertions(+), 47 deletions(-) diff --git a/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Abstractions.Tests/MorganStanley.ComposeUI.ModuleLoader.Abstractions.Tests.csproj b/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Abstractions.Tests/MorganStanley.ComposeUI.ModuleLoader.Abstractions.Tests.csproj index e134c5bb3..588edca04 100644 --- a/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Abstractions.Tests/MorganStanley.ComposeUI.ModuleLoader.Abstractions.Tests.csproj +++ b/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Abstractions.Tests/MorganStanley.ComposeUI.ModuleLoader.Abstractions.Tests.csproj @@ -10,6 +10,7 @@ + diff --git a/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Abstractions.Tests/StartupContextTests.cs b/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Abstractions.Tests/StartupContextTests.cs index f66010475..b83a49e38 100644 --- a/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Abstractions.Tests/StartupContextTests.cs +++ b/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Abstractions.Tests/StartupContextTests.cs @@ -10,6 +10,7 @@ // or implied. See the License for the specific language governing permissions // and limitations under the License. +using FluentAssertions; using Moq; namespace MorganStanley.ComposeUI.ModuleLoader.Abstractions.Tests; @@ -25,20 +26,21 @@ public void WhenAdd_AddedValuesCanBeRetrieved() new MyContextInfo { Name = "Test2" } }; - StartupContext context = new StartupContext(new StartRequest("test"), Mock.Of()); + var context = new StartupContext(new StartRequest("test"), Mock.Of()); context.AddProperty(expected[0]); context.AddProperty(expected[1]); var result = context.GetProperties(); - Assert.NotNull(result); - Assert.Equal(expected, result); + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(expected); } [Fact] public void GivenNullArgument_WhenAdd_ThrowsArgumentNullException() { - StartupContext context = new StartupContext(new StartRequest("test"), Mock.Of()); - Assert.Throws(() => context.AddProperty(null!)); + var context = new StartupContext(new StartRequest("test"), Mock.Of()); + var action = () => context.AddProperty(null!); + action.Should().Throw(); } private class MyContextInfo diff --git a/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/DependencyInjection/ServiceCollectionExtensionsTests.cs b/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/DependencyInjection/ServiceCollectionExtensionsTests.cs index 680b579c3..f4d062647 100644 --- a/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/DependencyInjection/ServiceCollectionExtensionsTests.cs +++ b/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/DependencyInjection/ServiceCollectionExtensionsTests.cs @@ -10,6 +10,7 @@ // or implied. See the License for the specific language governing permissions // and limitations under the License. +using FluentAssertions; using Microsoft.Extensions.DependencyInjection; using MorganStanley.ComposeUI.ModuleLoader.Runners; @@ -23,8 +24,8 @@ public void WhenAddModuleLoader_TypesAreAddedToServiceCollection() var services = new ServiceCollection() .AddModuleLoader(); - Assert.Equal(1, services.Count(s => s.ServiceType == typeof(IModuleLoader) && s.ImplementationType == typeof(ModuleLoader))); - Assert.Equal(1, services.Count(s => s.ServiceType == typeof(IModuleRunner) && s.ImplementationType == typeof(WebModuleRunner))); - Assert.Equal(1, services.Count(s => s.ServiceType == typeof(IModuleRunner) && s.ImplementationType == typeof(NativeModuleRunner))); + services.Count(s => s.ServiceType == typeof(IModuleLoader) && s.ImplementationType == typeof(ModuleLoader)).Should().Be(1); + services.Count(s => s.ServiceType == typeof(IModuleRunner) && s.ImplementationType == typeof(WebModuleRunner)).Should().Be(1); + services.Count(s => s.ServiceType == typeof(IModuleRunner) && s.ImplementationType == typeof(NativeModuleRunner)).Should().Be(1); } } diff --git a/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/ModuleInstanceTests.cs b/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/ModuleInstanceTests.cs index b193ba778..52989d79e 100644 --- a/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/ModuleInstanceTests.cs +++ b/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/ModuleInstanceTests.cs @@ -11,6 +11,7 @@ // and limitations under the License. +using FluentAssertions; using Moq; namespace MorganStanley.ComposeUI.ModuleLoader.Tests; @@ -22,17 +23,19 @@ public class ModuleInstanceTests [Fact] public void GivenNullArguments_WhenCtor_ThrowsArgumentNullException() { - Assert.Throws(() => new ModuleInstance(_testGuid, null!, new StartRequest("test"))); - Assert.Throws(() => new ModuleInstance(_testGuid, new Mock().Object, null!)); + var action1 = () => new ModuleInstance(_testGuid, null!, new StartRequest("test")); + action1.Should().Throw(); + + var action2 = () => new ModuleInstance(_testGuid, new Mock().Object, null!); + action2.Should().Throw(); } [Fact] public void WhenNewModuleInstance_GetProperties_ReturnsEmptyCollection() { var moduleInstance = new ModuleInstance(_testGuid, new Mock().Object, new StartRequest("test")); - var properties = moduleInstance.GetProperties(); - Assert.Empty(properties); + properties.Should().BeEmpty(); } [Fact] @@ -44,6 +47,6 @@ public void WhenSetProperties_GetPropertiesReturnThePropertiesAdded() moduleInstance.AddProperties(testProperties); var properties = moduleInstance.GetProperties(); - Assert.Equal(testProperties, properties); + testProperties.Should().BeEquivalentTo(properties); } } diff --git a/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/ModuleLoaderTests.cs b/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/ModuleLoaderTests.cs index 5d09f16c0..448e07b95 100644 --- a/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/ModuleLoaderTests.cs +++ b/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/ModuleLoaderTests.cs @@ -10,6 +10,7 @@ // or implied. See the License for the specific language governing permissions // and limitations under the License. +using FluentAssertions; using Microsoft.Extensions.DependencyInjection; using Moq; @@ -20,23 +21,28 @@ public class ModuleLoaderTests [Fact] public void GivenNullArguments_WhenCtor_ThrowsArgumentNullException() { - Assert.Throws(() => new ModuleLoader(null!, Enumerable.Empty(), Enumerable.Empty())); - Assert.Throws(() => new ModuleLoader(new Mock>().Object, null!, Enumerable.Empty())); - Assert.Throws(() => new ModuleLoader(new Mock>().Object, Enumerable.Empty(), null!)); + var action1 = () => new ModuleLoader(null!, Enumerable.Empty(), Enumerable.Empty()); + var action2 = () => new ModuleLoader(new Mock>().Object, null!, Enumerable.Empty()); + var action3 = () => new ModuleLoader(new Mock>().Object, Enumerable.Empty(), null!); + + action1.Should().Throw(); + action2.Should().Throw(); + action3.Should().Throw(); } [Fact] - public void GivenUnknownModuleId_WhenStart_ThrowsException() + public async void GivenUnknownModuleId_WhenStart_ThrowsException() { var moduleCatalogMock = new Mock(); moduleCatalogMock.Setup(c => c.GetManifest(It.IsAny())).Returns(Task.FromResult(null)); var moduleLoader = new ModuleLoader(new[] { moduleCatalogMock.Object }, Enumerable.Empty(), Enumerable.Empty()); - Assert.ThrowsAsync(async () => await moduleLoader.StartModule(new StartRequest("invalid"))); + var action = () => moduleLoader.StartModule(new StartRequest("invalid")); + await action.Should().ThrowAsync(); } [Fact] - public void WhenNoModuleRunnerAvailable_WhenStart_ThrowsException() + public async Task WhenNoModuleRunnerAvailable_WhenStart_ThrowsException() { var moduleManifestMock = new Mock(); moduleManifestMock.Setup(m => m.ModuleType).Returns("test"); @@ -46,7 +52,8 @@ public void WhenNoModuleRunnerAvailable_WhenStart_ThrowsException() testModuleRunnerMock.Setup(r => r.ModuleType).Returns("other"); var moduleLoader = new ModuleLoader(new[] { moduleCatalogMock.Object }, new[] { testModuleRunnerMock.Object }, Enumerable.Empty()); - Assert.ThrowsAsync(async () => await moduleLoader.StartModule(new StartRequest("valid"))); + var action = () => moduleLoader.StartModule(new StartRequest("valid")); + await action.Should().ThrowAsync(); } [Fact] @@ -77,13 +84,13 @@ public async Task StartModule_EndToEndTest() var serviceProvider = new ServiceCollection() .AddModuleLoader() - .AddSingleton(moduleCatalogMock.Object) + .AddSingleton>(new[] { moduleCatalogMock.Object }) .AddSingleton(startupActionMock.Object) .BuildServiceProvider(); var moduleLoader = serviceProvider.GetRequiredService(); - bool startingEventReceived = false; - bool startedEventReceived = false; + var startingEventReceived = false; + var startedEventReceived = false; moduleLoader.LifetimeEvents.Subscribe(x => { if (x.EventType == LifetimeEventType.Starting) @@ -95,20 +102,19 @@ public async Task StartModule_EndToEndTest() var startRequest = new StartRequest(moduleId); var moduleInstance = await moduleLoader.StartModule(startRequest); - Assert.NotNull(moduleInstance); - Assert.Equal(moduleManifestMock.Object, moduleInstance.Manifest); - Assert.Equal(startRequest, moduleInstance.StartRequest); + moduleInstance.Should().NotBeNull(); + moduleManifestMock.Object.Should().BeEquivalentTo(moduleInstance.Manifest); + startRequest.Should().BeEquivalentTo(moduleInstance.StartRequest); + var allProperties = moduleInstance.GetProperties(); - var webProperties = allProperties.OfType().Single(); - Assert.Equal(webManifestDetails.IconUrl, webProperties.IconUrl); - Assert.Equal(webManifestDetails.Url, webProperties.Url); + webManifestDetails.IconUrl.Should().BeEquivalentTo(webProperties.IconUrl); + webManifestDetails.Url.Should().BeEquivalentTo(webProperties.Url); var stringProperty = allProperties.OfType().Single(); - Assert.Equal(testProperty, stringProperty); - - Assert.True(startingEventReceived); - Assert.True(startedEventReceived); + testProperty.Should().BeEquivalentTo(stringProperty); + startingEventReceived.Should().BeTrue(); + startedEventReceived.Should().BeTrue(); } - } + } } \ No newline at end of file diff --git a/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/Runners/NativeModuleRunnerTests.cs b/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/Runners/NativeModuleRunnerTests.cs index 03e4fdbac..ae2661290 100644 --- a/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/Runners/NativeModuleRunnerTests.cs +++ b/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/Runners/NativeModuleRunnerTests.cs @@ -11,6 +11,8 @@ // and limitations under the License. using System.Diagnostics; +using FluentAssertions; +using FluentAssertions.Execution; using Moq; using MorganStanley.ComposeUI.ModuleLoader.Runners; using MorganStanley.ComposeUI.ModuleLoader.Tests.TestUtils; @@ -26,7 +28,7 @@ public class NativeModuleRunnerTests : IDisposable [Fact] public void ModuleType_is_Native() { - Assert.Equal(ModuleType.Native, _runner.ModuleType); + _runner.ModuleType.Should().Be(ModuleType.Native); } [Fact] @@ -53,13 +55,13 @@ public async Task ModuleIsLaunchedWithProvidedArguments() var processInfo = startupContext.GetOrAddProperty(() => { - Assert.Fail("No MainProcessInfo found"); + Execute.Assertion.FailWith("No MainProcessInfo found"); return null; }); _mainProcess = processInfo.MainProcess; var result = await _mainProcess.WaitForExitAsync(Timeout); - Assert.Equal($"Hello ComposeUI! I am {randomString}", result.Output.Trim()); + result.Output.Trim().Should().Be($"Hello ComposeUI! I am {randomString}"); } [Fact] @@ -86,13 +88,13 @@ public async Task AbsolutePathModuleIsLaunchedWithEnvironmentVariablesFromManife var processInfo = startupContext.GetOrAddProperty(() => { - Assert.Fail("No MainProcessInfo found"); + Execute.Assertion.FailWith("No MainProcessInfo found"); return null; }); _mainProcess = processInfo.MainProcess; var result = await _mainProcess.WaitForExitAsync(Timeout); - Assert.Contains($"{variableName}={randomString}", result.Output); + result.Output.Should().Contain($"{variableName}={randomString}"); } [Fact] @@ -119,13 +121,13 @@ public async Task RelativePathModuleIsLaunchedWithEnvironmentVariablesFromManife var processInfo = startupContext.GetOrAddProperty(() => { - Assert.Fail("No MainProcessInfo found"); + Execute.Assertion.FailWith("No MainProcessInfo found"); return null; }); _mainProcess = processInfo.MainProcess; var result = await _mainProcess.WaitForExitAsync(Timeout); - Assert.Contains($"{variableName}={randomString}", result.Output); + result.Output.Should().Contain($"{variableName}={randomString}"); } [Fact] @@ -156,13 +158,13 @@ await _runner.Start(startupContext, () => var processInfo = startupContext.GetOrAddProperty(() => { - Assert.Fail("No MainProcessInfo found"); + Execute.Assertion.FailWith("No MainProcessInfo found"); return null; }); _mainProcess = processInfo.MainProcess; var result = await _mainProcess.WaitForExitAsync(Timeout); - Assert.Contains($"{variableName}={randomString}", result.Output); + result.Output.Should().Contain($"{variableName}={randomString}"); } private Task RedirectMainProcessOutput(StartupContext startupContext) diff --git a/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/Runners/WebModuleRunnerTests.cs b/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/Runners/WebModuleRunnerTests.cs index 3935fedb8..7caa0c392 100644 --- a/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/Runners/WebModuleRunnerTests.cs +++ b/src/module-loader/dotnet/tests/MorganStanley.ComposeUI.ModuleLoader.Tests/Runners/WebModuleRunnerTests.cs @@ -11,6 +11,7 @@ // and limitations under the License. +using FluentAssertions; using Moq; using MorganStanley.ComposeUI.ModuleLoader.Runners; @@ -22,7 +23,7 @@ public class WebModuleRunnerTests public void ModuleType_Is_Web() { var runner = new WebModuleRunner(); - Assert.Equal(ModuleType.Web, runner.ModuleType); + runner.ModuleType.Should().Be(ModuleType.Web); } [Fact] @@ -40,10 +41,10 @@ public async Task WhenStart_WebStartupPropertiesAreAddedToStartupContext() await runner.Start(startupContext, MockPipeline); var result = startupContext.GetProperties(); - Assert.NotNull(result); + result.Should().NotBeNull(); var webProperties = result.OfType().Single(); - Assert.Equal(details.IconUrl, webProperties.IconUrl); - Assert.Equal(details.Url, webProperties.Url); + details.IconUrl.Should().BeEquivalentTo(webProperties.IconUrl); + details.Url.Should().BeEquivalentTo(webProperties.Url); } private class MockModuleManifest : IModuleManifest