-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for service lifetime instance
- Loading branch information
1 parent
087d4b3
commit 011cf61
Showing
3 changed files
with
81 additions
and
2 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
28 changes: 28 additions & 0 deletions
28
ShopifySharp.Extensions.DependencyInjection.Tests/TestExtensions.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,28 @@ | ||
namespace ShopifySharp.Extensions.DependencyInjection.Tests | ||
{ | ||
public static class ObjectExtensions | ||
{ | ||
private static readonly Dictionary<ServiceLifetime, bool> ServiceLifetimeInstanceValidation = new() | ||
{ | ||
{ ServiceLifetime.Scoped, true }, | ||
{ ServiceLifetime.Singleton, true }, | ||
{ ServiceLifetime.Transient, false } | ||
}; | ||
|
||
public static bool ValidLifetimeInstance(this object actual, object expceted, ServiceLifetime serviceLifetime) | ||
{ | ||
return ServiceLifetimeInstanceValidation.TryGetValue(serviceLifetime, out bool value) && value ? ReferenceEquals(actual, expceted) : !ReferenceEquals(actual, expceted); | ||
} | ||
} | ||
|
||
public static class ServiceProviderExtensions | ||
{ | ||
public static TestServiceLifetime<T> GetServiceInstances<T>(this ServiceProvider? serviceProvider) where T : class | ||
{ | ||
var service1 = serviceProvider?.GetService<T>(); | ||
var service2 = serviceProvider?.GetService<T>(); | ||
|
||
return new TestServiceLifetime<T>(service1, service2); | ||
} | ||
} | ||
} |