|
1 | 1 | using System;
|
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Linq;
|
3 | 4 | using System.Reflection;
|
4 | 5 | using FluentAssertions;
|
@@ -27,7 +28,68 @@ public void GetServiceCollection_HappyPath_ResolvesCorrectServiceCollection()
|
27 | 28 | var testService = serviceProvider.GetRequiredService<ITestInterface>();
|
28 | 29 | testService.Name.Should().Be("ValidStartWithAutoRegister");
|
29 | 30 | }
|
30 |
| - |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void GetServiceCollection_MethodIsVoid_ThrowsInvalidScenarioDependenciesException() |
| 34 | + { |
| 35 | + // Arrange |
| 36 | + var testRunnerManagerMock = CreateTestRunnerManagerMock(typeof(InvalidStartVoid)); |
| 37 | + var sut = new ServiceCollectionFinder(testRunnerManagerMock.Object); |
| 38 | + |
| 39 | + // Act |
| 40 | + var act = () => sut.GetServiceCollection(); |
| 41 | + |
| 42 | + // Assert |
| 43 | + act.Should().Throw<InvalidScenarioDependenciesException>() |
| 44 | + .WithMessage("[ScenarioDependencies] should return IServiceCollection but the method doesn't return a value."); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void GetServiceCollection_MethodReturnsNull_ThrowsInvalidScenarioDependenciesException() |
| 49 | + { |
| 50 | + // Arrange |
| 51 | + var testRunnerManagerMock = CreateTestRunnerManagerMock(typeof(InvalidStartNull)); |
| 52 | + var sut = new ServiceCollectionFinder(testRunnerManagerMock.Object); |
| 53 | + |
| 54 | + // Act |
| 55 | + var act = () => sut.GetServiceCollection(); |
| 56 | + |
| 57 | + // Assert |
| 58 | + act.Should().Throw<InvalidScenarioDependenciesException>() |
| 59 | + .WithMessage("[ScenarioDependencies] should return IServiceCollection but returned null."); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void GetServiceCollection_MethodReturnsInvalidType_ThrowsInvalidScenarioDependenciesException() |
| 65 | + { |
| 66 | + // Arrange |
| 67 | + var testRunnerManagerMock = CreateTestRunnerManagerMock(typeof(InvalidStartWrongType)); |
| 68 | + var sut = new ServiceCollectionFinder(testRunnerManagerMock.Object); |
| 69 | + |
| 70 | + // Act |
| 71 | + var act = () => sut.GetServiceCollection(); |
| 72 | + |
| 73 | + // Assert |
| 74 | + act.Should().Throw<InvalidScenarioDependenciesException>() |
| 75 | + .WithMessage("[ScenarioDependencies] should return IServiceCollection but returned System.Collections.Generic.List`1[System.String]."); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void GetServiceCollection_NotFound_ThrowsMissingScenarioDependenciesException() |
| 80 | + { |
| 81 | + // Arrange |
| 82 | + var testRunnerManagerMock = CreateTestRunnerManagerMock(typeof(ServiceCollectionFinderTests)); |
| 83 | + var sut = new ServiceCollectionFinder(testRunnerManagerMock.Object); |
| 84 | + |
| 85 | + // Act |
| 86 | + var act = () => sut.GetServiceCollection(); |
| 87 | + |
| 88 | + // Assert |
| 89 | + act.Should().Throw<MissingScenarioDependenciesException>() |
| 90 | + .WithMessage("No method marked with [ScenarioDependencies] attribute found. It should be a (public or non-public) static method. Scanned assemblies: Reqnroll.PluginTests."); |
| 91 | + } |
| 92 | + |
31 | 93 | [Fact]
|
32 | 94 | public void GetServiceCollection_AutoRegisterBindingsTrue_RegisterBindingsAsScoped()
|
33 | 95 | {
|
@@ -62,6 +124,11 @@ private static Mock<ITestRunnerManager> CreateTestRunnerManagerMock(params Type[
|
62 | 124 | {
|
63 | 125 | var assemblyMock = new Mock<Assembly>();
|
64 | 126 | assemblyMock.Setup(m => m.GetTypes()).Returns(types.ToArray());
|
| 127 | + if (types.Length > 0) |
| 128 | + { |
| 129 | + var assembly = types[0].Assembly; |
| 130 | + assemblyMock.Setup(m => m.GetName()).Returns(assembly.GetName()); |
| 131 | + } |
65 | 132 |
|
66 | 133 | var testRunnerManagerMock = new Mock<ITestRunnerManager>();
|
67 | 134 | testRunnerManagerMock.Setup(m => m.BindingAssemblies).Returns([assemblyMock.Object]);
|
@@ -94,6 +161,33 @@ public static IServiceCollection GetServices()
|
94 | 161 | return serviceCollection;
|
95 | 162 | }
|
96 | 163 | }
|
| 164 | + private class InvalidStartVoid |
| 165 | + { |
| 166 | + [ScenarioDependencies] |
| 167 | + public static void GetServices() |
| 168 | + { |
| 169 | + var serviceCollection = new ServiceCollection(); |
| 170 | + serviceCollection.AddSingleton<ITestInterface>(new TestInterface("ValidStartWithAutoRegister")); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private class InvalidStartNull |
| 175 | + { |
| 176 | + [ScenarioDependencies] |
| 177 | + public static IServiceCollection GetServices() |
| 178 | + { |
| 179 | + return null; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private class InvalidStartWrongType |
| 184 | + { |
| 185 | + [ScenarioDependencies] |
| 186 | + public static object GetServices() |
| 187 | + { |
| 188 | + return new List<string>(); |
| 189 | + } |
| 190 | + } |
97 | 191 | [Binding]
|
98 | 192 | private class Binding1;
|
99 | 193 | }
|
0 commit comments