Skip to content

Commit

Permalink
Refactoring unit test codes
Browse files Browse the repository at this point in the history
  • Loading branch information
gayaK committed Apr 26, 2020
1 parent 5c14ec4 commit e8622d6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,31 @@

namespace Prism.Container.Wpf.Tests.Ioc
{
public class ContainerProviderExtensionFixture
public class ContainerProviderExtensionFixture : IDisposable
{
public ContainerProviderExtensionFixture()
{
Initalize();
}

private static bool _initialized;

private static readonly MockService _unnamedService = new MockService();
private static readonly IReadOnlyDictionary<string, MockService> _namedServiceDictionary = new Dictionary<string, MockService>
{
["A"] = new MockService(),
["B"] = new MockService(),
};

private void Initalize()
public ContainerProviderExtensionFixture()
{
if (_initialized)
{
return;
}

ContainerLocator.ResetContainer();
#if DryIoc
var container = new global::DryIoc.Container(global::DryIoc.Rules.Default.WithAutoConcreteTypeResolution());
ContainerLocator.SetContainerExtension(() => new Prism.DryIoc.DryIocContainerExtension(container));
#elif Unity
ContainerLocator.SetContainerExtension(() => new Prism.Unity.UnityContainerExtension());
#endif
ContainerLocator.Current.RegisterInstance<IService>(_unnamedService);
ContainerLocator.SetContainerExtension(() => ContainerHelper.CreateContainerExtension());
var containerExtension = ContainerLocator.Current;
containerExtension.RegisterInstance<IService>(_unnamedService);
foreach (var kvp in _namedServiceDictionary)
{
ContainerLocator.Current.RegisterInstance<IService>(kvp.Value, kvp.Key);
containerExtension.RegisterInstance<IService>(kvp.Value, kvp.Key);
}
containerExtension.FinalizeExtension();
}

_initialized = true;
public void Dispose()
{
ContainerLocator.ResetContainer();
}

[Fact]
Expand Down Expand Up @@ -111,11 +99,11 @@ public void CanResolveServiceFromMarkupExtension()
{
var xaml =
@"<Window
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:prism=""clr-namespace:Prism.Ioc;assembly=Prism.Wpf""
xmlns:mocks=""clr-namespace:Prism.IocContainer.Wpf.Tests.Support.Mocks;assembly=Prism.IocContainer.Wpf.Tests.Support""
DataContext=""{prism:ContainerProvider 'mocks:IService'}"" />";
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:prism='http://prismlibrary.com/'
xmlns:mocks='clr-namespace:Prism.IocContainer.Wpf.Tests.Support.Mocks;assembly=Prism.IocContainer.Wpf.Tests.Support'
DataContext='{prism:ContainerProvider mocks:IService}' />";

using (var reader = new StringReader(xaml))
{
Expand All @@ -130,12 +118,12 @@ public void CanResolveServiceFromXmlElement()
{
var xaml =
@"<Window
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:prism=""clr-namespace:Prism.Ioc;assembly=Prism.Wpf""
xmlns:mocks=""clr-namespace:Prism.IocContainer.Wpf.Tests.Support.Mocks;assembly=Prism.IocContainer.Wpf.Tests.Support"">
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:prism='clr-namespace:Prism.Ioc;assembly=Prism.Wpf'
xmlns:mocks='clr-namespace:Prism.IocContainer.Wpf.Tests.Support.Mocks;assembly=Prism.IocContainer.Wpf.Tests.Support'>
<Window.DataContext>
<prism:ContainerProvider Type=""mocks:IService"" />
<prism:ContainerProvider Type='mocks:IService' />
</Window.DataContext>
</Window>";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>TRACE;DryIoc</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DryIoc.dll" Version="4.1.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
Expand Down
4 changes: 0 additions & 4 deletions tests/Wpf/Prism.Unity.Wpf.Tests/Prism.Unity.Wpf.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>TRACE;Unity</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Moq" Version="4.13.1" />
Expand Down

0 comments on commit e8622d6

Please sign in to comment.