diff --git a/README.md b/README.md index 271f8d7..9c5bb11 100644 --- a/README.md +++ b/README.md @@ -86,4 +86,23 @@ public class AutoMockerTestContextTests : AutoMockerTestContext } ``` +When a certain type should not be handled by AutoMocker, a list of types can be provided via the constructor to exclude: +``` cs +public class ExcludeTypesTests : AutoMockerTestContext +{ + public ExcludeTypesTests() : base(typeof(IMyOtherDependency)) + { } + + [Fact] + public void WhenUnregisteredDependencyIsNotHandledByAutoMocker_ThrowsInvalidOperationException() + { + // Arrange + Mocker.Use(x => x.GetSomeValue() == "Hello World"); + + // Act & Assert + Assert.Throws(() => RenderComponent()); + } +} +``` + [0]:https://github.com/moq/Moq.AutoMocker \ No newline at end of file diff --git a/bunit.AutoMocker.Tests/AutoMockerTestBase.cs b/bunit.AutoMocker.Tests/AutoMockerTestBase.cs new file mode 100644 index 0000000..8be82ea --- /dev/null +++ b/bunit.AutoMocker.Tests/AutoMockerTestBase.cs @@ -0,0 +1,8 @@ +using Bunit.AutoMocker; +using Microsoft.AspNetCore.Components; + +namespace bunit.AutoMocker.Tests; + +public abstract class AutoMockerTestBase(params Type[] typesToExclude) + : AutoMockerTestContext(typesToExclude.Union(new[] { typeof(IComponentActivator) }).ToArray()) +{ } diff --git a/bunit.AutoMocker.Tests/AutoMockerTestContextTests.cs b/bunit.AutoMocker.Tests/AutoMockerTestContextTests.cs index af404d2..30486d2 100644 --- a/bunit.AutoMocker.Tests/AutoMockerTestContextTests.cs +++ b/bunit.AutoMocker.Tests/AutoMockerTestContextTests.cs @@ -1,11 +1,8 @@ using bunit.BlazorTestApp.Components; -using Bunit; -using Bunit.AutoMocker; -using Microsoft.Extensions.DependencyInjection; namespace bunit.AutoMocker.Tests; -public class AutoMockerTestContextTests : AutoMockerTestContext +public class AutoMockerTestContextTests : AutoMockerTestBase { [Fact] public void CreatesUnregisteredDependencies() @@ -20,44 +17,3 @@ public void CreatesUnregisteredDependencies() Assert.Contains(cut.Markup, "Hello World"); } } - -public class WithoutAutoMockerTests : TestContext -{ - [Fact] - public void Fails_WhenDependenciesAreUnregistered() - { - Assert.Throws(() => RenderComponent()); - } - - [Fact] - public void Fails_WhenAllOnlyUsedDependencyIsRegisterd() - { - // Arrange - var myDependency = new Moq.Mock(); - myDependency.Setup(x => x.GetSomeValue()).Returns("Hello World"); - - Services.AddSingleton(myDependency.Object); - - // Act & Assert - Assert.Throws(() => RenderComponent()); - } - - [Fact] - public void Succeeds_WhenAllDependenciesAreRegistered() - { - // Arrange - var myDependency = new Moq.Mock(); - myDependency.Setup(x => x.GetSomeValue()).Returns("Hello World"); - - Services.AddSingleton(myDependency.Object); - - var myOtherDependency = new Moq.Mock(); - Services.AddSingleton(myOtherDependency.Object); - - // Act - var cut = RenderComponent(); - - // Assert - Assert.Contains(cut.Markup, "Hello World"); - } -} \ No newline at end of file diff --git a/bunit.AutoMocker.Tests/ExcludeTypesTests.cs b/bunit.AutoMocker.Tests/ExcludeTypesTests.cs new file mode 100644 index 0000000..73f8ba9 --- /dev/null +++ b/bunit.AutoMocker.Tests/ExcludeTypesTests.cs @@ -0,0 +1,19 @@ +using bunit.BlazorTestApp.Components; + +namespace bunit.AutoMocker.Tests; + +public sealed class ExcludeTypesTests : AutoMockerTestBase +{ + public ExcludeTypesTests() : base(typeof(IMyOtherDependency)) + { } + + [Fact] + public void WhenUnregisteredDependencyIsNotHandledByAutoMocker_ThrowsInvalidOperationException() + { + // Arrange + Mocker.Use(x => x.GetSomeValue() == "Hello World"); + + // Act & Assert + Assert.Throws(() => RenderComponent()); + } +} diff --git a/bunit.AutoMocker.Tests/WithoutAutoMockerTests.cs b/bunit.AutoMocker.Tests/WithoutAutoMockerTests.cs new file mode 100644 index 0000000..2032927 --- /dev/null +++ b/bunit.AutoMocker.Tests/WithoutAutoMockerTests.cs @@ -0,0 +1,46 @@ +using bunit.BlazorTestApp.Components; +using Bunit; +using Microsoft.Extensions.DependencyInjection; + +namespace bunit.AutoMocker.Tests; + +public class WithoutAutoMockerTests : TestContext +{ + [Fact] + public void Fails_WhenDependenciesAreUnregistered() + { + Assert.Throws(() => RenderComponent()); + } + + [Fact] + public void Fails_WhenAllOnlyUsedDependencyIsRegisterd() + { + // Arrange + var myDependency = new Moq.Mock(); + myDependency.Setup(x => x.GetSomeValue()).Returns("Hello World"); + + Services.AddSingleton(myDependency.Object); + + // Act & Assert + Assert.Throws(() => RenderComponent()); + } + + [Fact] + public void Succeeds_WhenAllDependenciesAreRegistered() + { + // Arrange + var myDependency = new Moq.Mock(); + myDependency.Setup(x => x.GetSomeValue()).Returns("Hello World"); + + Services.AddSingleton(myDependency.Object); + + var myOtherDependency = new Moq.Mock(); + Services.AddSingleton(myOtherDependency.Object); + + // Act + var cut = RenderComponent(); + + // Assert + Assert.Contains(cut.Markup, "Hello World"); + } +} \ No newline at end of file diff --git a/bunit.AutoMocker.Tests/bunit.AutoMocker.Tests.csproj b/bunit.AutoMocker.Tests/bunit.AutoMocker.Tests.csproj index 9ecf0ba..cb8ed84 100644 --- a/bunit.AutoMocker.Tests/bunit.AutoMocker.Tests.csproj +++ b/bunit.AutoMocker.Tests/bunit.AutoMocker.Tests.csproj @@ -10,10 +10,16 @@ - - - - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/bunit.AutoMocker/AutoMockerServiceProvider.cs b/bunit.AutoMocker/AutoMockerServiceProvider.cs index f091dea..0e949a6 100644 --- a/bunit.AutoMocker/AutoMockerServiceProvider.cs +++ b/bunit.AutoMocker/AutoMockerServiceProvider.cs @@ -1,13 +1,18 @@ using System; +using System.Collections.Generic; +using System.Linq; namespace Bunit.AutoMocker; -public class AutoMockerServiceProvider(Moq.AutoMock.AutoMocker mocker) : IServiceProvider +public class AutoMockerServiceProvider(Moq.AutoMock.AutoMocker mocker, params Type[] typesToExclude) : IServiceProvider { - private readonly Moq.AutoMock.AutoMocker Mocker = mocker; + private readonly HashSet _typesToExclude = typesToExclude.ToHashSet(); + private readonly Moq.AutoMock.AutoMocker _mocker = mocker; - public object GetService(Type serviceType) + public object? GetService(Type serviceType) { - return Mocker.Get(serviceType); + return _typesToExclude.Contains(serviceType) + ? null + : _mocker.Get(serviceType); } } \ No newline at end of file diff --git a/bunit.AutoMocker/AutoMockerTestContext.cs b/bunit.AutoMocker/AutoMockerTestContext.cs index d6d7a1b..c0ed20f 100644 --- a/bunit.AutoMocker/AutoMockerTestContext.cs +++ b/bunit.AutoMocker/AutoMockerTestContext.cs @@ -1,14 +1,16 @@ -namespace Bunit.AutoMocker; +using System; + +namespace Bunit.AutoMocker; public abstract class AutoMockerTestContext : TestContext { protected readonly Moq.AutoMock.AutoMocker Mocker = new(); - public AutoMockerTestContext() : this(new Moq.AutoMock.AutoMocker()) { } + public AutoMockerTestContext(params Type[] typesToExclude) : this(new Moq.AutoMock.AutoMocker(), typesToExclude) { } - public AutoMockerTestContext(Moq.AutoMock.AutoMocker mocker) + public AutoMockerTestContext(Moq.AutoMock.AutoMocker mocker, params Type[] typesToExclude) { Mocker = mocker; - Services.AddFallbackServiceProvider(new AutoMockerServiceProvider(Mocker)); + Services.AddFallbackServiceProvider(new AutoMockerServiceProvider(Mocker, typesToExclude)); } } diff --git a/bunit.AutoMocker/bunit.AutoMocker.csproj b/bunit.AutoMocker/bunit.AutoMocker.csproj index 4c761d4..bd5d5ae 100644 --- a/bunit.AutoMocker/bunit.AutoMocker.csproj +++ b/bunit.AutoMocker/bunit.AutoMocker.csproj @@ -4,10 +4,11 @@ netstandard2.1 latest Snerte.bunit.AutoMocker - 0.0.2 + 0.0.3 Maarten Göertz This pacakge combines AutoMocker with bunit https://github.com/snerte/bunit.AutoMocker + enable @@ -16,7 +17,7 @@ - + @@ -25,7 +26,7 @@ - +