-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1240 from bUnit-dev/release/v1.24
Release of new minor version v1.24
- Loading branch information
Showing
74 changed files
with
719 additions
and
2,757 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Bunit.Docs.Samples; | ||
|
||
public sealed class CustomServiceProvider : IServiceProvider, IServiceScopeFactory, IServiceScope | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public CustomServiceProvider(IServiceCollection serviceDescriptors) | ||
=> _serviceProvider = serviceDescriptors.BuildServiceProvider(); | ||
|
||
public object GetService(Type serviceType) | ||
{ | ||
if (serviceType == typeof(IServiceScope) || serviceType == typeof(IServiceScopeFactory)) | ||
return this; | ||
|
||
if (serviceType == typeof(DummyService)) | ||
return new DummyService(); | ||
|
||
return _serviceProvider.GetService(serviceType); | ||
} | ||
|
||
void IDisposable.Dispose() { } | ||
public IServiceScope CreateScope() => this; | ||
IServiceProvider IServiceScope.ServiceProvider => this; | ||
} | ||
|
||
public sealed class CustomServiceProviderFactoryContainerBuilder | ||
{ | ||
private readonly IServiceCollection _serviceDescriptors; | ||
|
||
public CustomServiceProviderFactoryContainerBuilder(IServiceCollection serviceDescriptors) | ||
=> this._serviceDescriptors = serviceDescriptors; | ||
|
||
public IServiceProvider Build() | ||
=> new CustomServiceProvider(_serviceDescriptors); | ||
} | ||
|
||
public sealed class CustomServiceProviderFactory : IServiceProviderFactory<CustomServiceProviderFactoryContainerBuilder> | ||
{ | ||
public CustomServiceProviderFactoryContainerBuilder CreateBuilder(IServiceCollection services) | ||
=> new CustomServiceProviderFactoryContainerBuilder(services); | ||
|
||
public IServiceProvider CreateServiceProvider(CustomServiceProviderFactoryContainerBuilder containerBuilder) | ||
=> containerBuilder.Build(); | ||
} |
89 changes: 89 additions & 0 deletions
89
docs/samples/tests/xunit/CustomServiceProviderFactoryUsage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using Autofac; | ||
using Autofac.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace Bunit.Docs.Samples; | ||
public class CustomServiceProviderFactoryUsage : TestContext | ||
{ | ||
[Fact] | ||
public void CustomServiceProviderViaFactoryReturns() | ||
{ | ||
Services.UseServiceProviderFactory(new CustomServiceProviderFactory()); | ||
|
||
var dummyService = Services.GetService<DummyService>(); | ||
|
||
Assert.NotNull(dummyService); | ||
} | ||
|
||
[Fact] | ||
public void CustomServiceProviderViaDelegateReturns() | ||
{ | ||
Services.UseServiceProviderFactory(x => new CustomServiceProvider(x)); | ||
|
||
var dummyService = Services.GetService<DummyService>(); | ||
|
||
Assert.NotNull(dummyService); | ||
} | ||
|
||
[Fact] | ||
public void AutofacServiceProviderViaFactoryReturns() | ||
{ | ||
void ConfigureContainer(ContainerBuilder containerBuilder) | ||
{ | ||
containerBuilder | ||
.RegisterType<DummyService>() | ||
.AsSelf(); | ||
} | ||
|
||
Services.UseServiceProviderFactory(new AutofacServiceProviderFactory(ConfigureContainer)); | ||
|
||
//get a service which was installed in the Autofac ContainerBuilder | ||
|
||
var dummyService = Services.GetService<DummyService>(); | ||
|
||
Assert.NotNull(dummyService); | ||
|
||
//get a service which was installed in the bUnit ServiceCollection | ||
|
||
var testContextBase = Services.GetService<TestContextBase>(); | ||
|
||
Assert.NotNull(testContextBase); | ||
Assert.Equal(this, testContextBase); | ||
} | ||
|
||
[Fact] | ||
public void AutofacServiceProviderViaDelegateReturns() | ||
{ | ||
ILifetimeScope ConfigureContainer(IServiceCollection services) | ||
{ | ||
var containerBuilder = new ContainerBuilder(); | ||
|
||
containerBuilder | ||
.RegisterType<DummyService>() | ||
.AsSelf(); | ||
|
||
containerBuilder.Populate(services); | ||
|
||
return containerBuilder.Build(); | ||
} | ||
|
||
Services.UseServiceProviderFactory(x => new AutofacServiceProvider(ConfigureContainer(x))); | ||
|
||
//get a service which was installed in the Autofac ContainerBuilder | ||
|
||
var dummyService = Services.GetService<DummyService>(); | ||
|
||
Assert.NotNull(dummyService); | ||
|
||
//get a service which was installed in the bUnit ServiceCollection | ||
|
||
var testContextBase = Services.GetService<TestContextBase>(); | ||
|
||
Assert.NotNull(testContextBase); | ||
Assert.Equal(this, testContextBase); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.