Skip to content

Commit

Permalink
Switch to NSubstitute.
Browse files Browse the repository at this point in the history
  • Loading branch information
tillig committed Mar 12, 2024
1 parent 6aa45f4 commit 5e8dc65
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</PropertyGroup>

<ItemGroup>
<Using Include="Moq" />
<Using Include="NSubstitute" />
<Using Include="Xunit" />
</ItemGroup>

Expand Down Expand Up @@ -45,7 +45,7 @@
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="8.0.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ public class AutofacMultitenantWebHostBuilderExtensionsTests
[Fact]
public void UseAutofacMultitenantRequestServices_AddsHttpContextAccessor()
{
var webHostBuilder = new Mock<IWebHostBuilder>();
var webHostBuilder = Substitute.For<IWebHostBuilder>();

var services = new ServiceCollection();
webHostBuilder
.Setup(x => x.ConfigureServices(It.IsAny<Action<IServiceCollection>>()))
.Callback<Action<IServiceCollection>>(s => s(services));

webHostBuilder.Object.UseAutofacMultitenantRequestServices();
webHostBuilder.ConfigureServices(Arg.Do<Action<IServiceCollection>>(s => s(services)));
webHostBuilder.UseAutofacMultitenantRequestServices();

var serviceProvider = services.BuildServiceProvider();
var accessor = serviceProvider.GetService<IHttpContextAccessor>();
Expand All @@ -30,14 +27,12 @@ public void UseAutofacMultitenantRequestServices_AddsHttpContextAccessor()
[Fact]
public void UseAutofacMultitenantRequestServices_AddsStartupFilter()
{
var webHostBuilder = new Mock<IWebHostBuilder>();
var webHostBuilder = Substitute.For<IWebHostBuilder>();

var services = new ServiceCollection();
webHostBuilder
.Setup(x => x.ConfigureServices(It.IsAny<Action<IServiceCollection>>()))
.Callback<Action<IServiceCollection>>(s => s(services));
webHostBuilder.ConfigureServices(Arg.Do<Action<IServiceCollection>>(s => s(services)));

webHostBuilder.Object.UseAutofacMultitenantRequestServices();
webHostBuilder.UseAutofacMultitenantRequestServices();

var serviceProvider = services.BuildServiceProvider();
var filter = serviceProvider.GetService<IStartupFilter>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class MultitenantRequestServicesMiddlewareTests
[Fact]
public async Task Invoke_DoesNotOverrideExistingHttpContextOnAccessor()
{
var accessor = Mock.Of<IHttpContextAccessor>();
var accessor = new SimpleHttpAccessor();
accessor.HttpContext = new DefaultHttpContext();
var next = new RequestDelegate(ctx => Task.CompletedTask);
var context = CreateContext();
Expand All @@ -30,8 +30,8 @@ public async Task Invoke_DoesNotOverrideExistingHttpContextOnAccessor()
[Fact]
public async Task Invoke_ReplacesRequestServices()
{
var accessor = Mock.Of<IHttpContextAccessor>();
var originalFeature = Mock.Of<IServiceProvidersFeature>();
var accessor = new SimpleHttpAccessor();
var originalFeature = Substitute.For<IServiceProvidersFeature>();
var next = new RequestDelegate(ctx =>
{
// When the next delegate is invoked, it should get
Expand All @@ -56,7 +56,7 @@ public async Task Invoke_ReplacesRequestServices()
[Fact]
public async Task Invoke_SetsHttpContextOnAccessor()
{
var accessor = Mock.Of<IHttpContextAccessor>();
var accessor = new SimpleHttpAccessor();
var next = new RequestDelegate(ctx => Task.CompletedTask);
var context = CreateContext();
var mtc = CreateServiceProvider().GetRequiredService<MultitenantContainer>();
Expand All @@ -79,7 +79,7 @@ private static IServiceProvider CreateServiceProvider()

private static MultitenantContainer CreateContainer(IContainer container)
{
var mtc = new MultitenantContainer(Mock.Of<ITenantIdentificationStrategy>(), container);
var mtc = new MultitenantContainer(Substitute.For<ITenantIdentificationStrategy>(), container);
return mtc;
}

Expand All @@ -90,6 +90,11 @@ private static DefaultHttpContext CreateContext()
return context;
}

private sealed class SimpleHttpAccessor : IHttpContextAccessor
{
public HttpContext? HttpContext { get; set; }
}

private sealed class TestHttpResponseFeature : HttpResponseFeature
{
public override void OnCompleted(Func<object, Task> callback, object state)
Expand Down

0 comments on commit 5e8dc65

Please sign in to comment.