-
-
Notifications
You must be signed in to change notification settings - Fork 749
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added "AddDataLoader<TService, TImplementation>" overload that takes …
…a factory (#7674)
- Loading branch information
1 parent
a390e46
commit 09211ff
Showing
2 changed files
with
69 additions
and
0 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
56 changes: 56 additions & 0 deletions
56
...enDonut/test/Core.Tests/DependencyInjection/DataLoaderServiceCollectionExtensionsTests.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,56 @@ | ||
using GreenDonut; | ||
using Xunit; | ||
using static GreenDonut.TestHelpers; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public class DataLoaderServiceCollectionExtensionsTests | ||
{ | ||
[Fact] | ||
public void ImplFactoryIsCalledWhenServiceIsResolved() | ||
{ | ||
// arrange | ||
var factoryCalled = false; | ||
var fetch = CreateFetch<string, string>(); | ||
var services = new ServiceCollection() | ||
.AddScoped<IBatchScheduler, ManualBatchScheduler>() | ||
.AddDataLoader(sp => | ||
{ | ||
factoryCalled = true; | ||
return new DataLoader<string, string>(fetch, sp.GetRequiredService<IBatchScheduler>()); | ||
}); | ||
var scope = services.BuildServiceProvider().CreateScope(); | ||
|
||
// act | ||
var dataLoader = scope.ServiceProvider.GetRequiredService<DataLoader<string, string>>(); | ||
|
||
// assert | ||
Assert.NotNull(dataLoader); | ||
Assert.True(factoryCalled); | ||
} | ||
|
||
[Fact] | ||
public void InterfaceImplFactoryIsCalledWhenServiceIsResolved() | ||
{ | ||
// arrange | ||
var factoryCalled = false; | ||
var fetch = CreateFetch<string, string>(); | ||
var services = new ServiceCollection() | ||
.AddScoped<IBatchScheduler, ManualBatchScheduler>() | ||
.AddDataLoader<IDataLoader<string, string>, DataLoader<string, string>>(sp => | ||
{ | ||
factoryCalled = true; | ||
return new DataLoader<string, string>(fetch, sp.GetRequiredService<IBatchScheduler>()); | ||
}); | ||
var scope = services.BuildServiceProvider().CreateScope(); | ||
|
||
// act | ||
var dataLoader = scope.ServiceProvider.GetRequiredService<DataLoader<string, string>>(); | ||
var asInterface = scope.ServiceProvider.GetRequiredService<IDataLoader<string, string>>(); | ||
|
||
// assert | ||
Assert.NotNull(dataLoader); | ||
Assert.NotNull(asInterface); | ||
Assert.True(factoryCalled); | ||
} | ||
} |