Skip to content

Commit c0b11ba

Browse files
Avoid external dependency in HostBuilder_CanCustomizeServiceFactory test
1 parent 1cdad4c commit c0b11ba

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

src/Components/test/Microsoft.AspNetCore.Components.Browser.Test/Hosting/WebAssemblyHostBuilderTest.cs

+43-17
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Text;
7-
using Autofac;
8-
using Autofac.Extensions.DependencyInjection;
97
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.JSInterop;
109
using Xunit;
1110

1211
namespace Microsoft.AspNetCore.Components.Hosting
@@ -81,42 +80,69 @@ public void ConfigureServices(IServiceCollection services)
8180
}
8281

8382
[Fact]
84-
public void HostBuilder_ServiceFactory_Autofac()
83+
public void HostBuilder_CanCustomizeServiceFactory()
8584
{
8685
// Arrange
8786
var builder = new WebAssemblyHostBuilder();
88-
builder.ConfigureServices((c, s) => s.AddSingleton<IServiceProviderFactory<IServiceCollection>>(new MyServiceProviderFactory()));
87+
builder.ConfigureServices((c, s) =>
88+
{
89+
s.AddSingleton<IServiceProviderFactory<IServiceCollection>>(
90+
new TestServiceProviderFactory());
91+
});
8992

9093
// Act
9194
var host = builder.Build();
9295

9396
// Assert
94-
Assert.IsType<AutofacServiceProvider>(host.Services);
97+
Assert.IsType<TestServiceProvider>(host.Services);
9598
}
9699

97-
private class MyServiceProviderFactory : IServiceProviderFactory<IServiceCollection>
100+
private class TestServiceProvider : IServiceProvider
98101
{
99-
public IServiceCollection CreateBuilder(IServiceCollection services)
102+
private readonly IServiceProvider _underlyingProvider;
103+
104+
public TestServiceProvider(IServiceProvider underlyingProvider)
100105
{
101-
return new MyServiceCollection(services);
106+
_underlyingProvider = underlyingProvider;
102107
}
103108

104-
public IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection)
109+
public object GetService(Type serviceType)
105110
{
106-
Assert.IsType<MyServiceCollection>(serviceCollection);
107-
var containerBuilder = new ContainerBuilder();
108-
containerBuilder.Populate(serviceCollection);
109-
var container = containerBuilder.Build();
110-
return new AutofacServiceProvider(container);
111+
if (serviceType == typeof(IWebAssemblyHost))
112+
{
113+
// Since the test will make assertions about the resulting IWebAssemblyHost,
114+
// show that custom DI containers have the power to substitute themselves
115+
// as the IServiceProvider
116+
return new WebAssemblyHost(
117+
this, _underlyingProvider.GetRequiredService<IJSRuntime>());
118+
}
119+
else
120+
{
121+
return _underlyingProvider.GetService(serviceType);
122+
}
111123
}
112124
}
113125

114-
private class MyServiceCollection : List<ServiceDescriptor>, IServiceCollection
126+
private class TestServiceProviderFactory : IServiceProviderFactory<IServiceCollection>
115127
{
116-
public MyServiceCollection(IEnumerable<ServiceDescriptor> collection) : base(collection)
128+
public IServiceCollection CreateBuilder(IServiceCollection services)
117129
{
130+
return new TestServiceCollection(services);
118131
}
119-
}
120132

133+
public IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection)
134+
{
135+
Assert.IsType<TestServiceCollection>(serviceCollection);
136+
return new TestServiceProvider(serviceCollection.BuildServiceProvider());
137+
}
138+
139+
class TestServiceCollection : List<ServiceDescriptor>, IServiceCollection
140+
{
141+
public TestServiceCollection(IEnumerable<ServiceDescriptor> collection)
142+
: base(collection)
143+
{
144+
}
145+
}
146+
}
121147
}
122148
}

src/Components/test/Microsoft.AspNetCore.Components.Browser.Test/Microsoft.AspNetCore.Components.Browser.Test.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.3.1" />
1110
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
1211
<PackageReference Include="xunit" Version="2.3.1" />
1312
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />

0 commit comments

Comments
 (0)