ChineseChess/ChineseChess.GUI.Tests.xUnit/PagesTests.cs
2021-05-30 22:57:53 +08:00

99 lines
3.3 KiB
C#

using System.IO;
using System.Reflection;
using ChineseChess.GUI.Contracts.Services;
using ChineseChess.GUI.Core.Contracts.Services;
using ChineseChess.GUI.Core.Services;
using ChineseChess.GUI.Models;
using ChineseChess.GUI.Services;
using ChineseChess.GUI.ViewModels;
using ChineseChess.GUI.Views;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;
namespace ChineseChess.GUI.Tests.XUnit
{
public class PagesTests
{
private readonly IHost _host;
public PagesTests()
{
var appLocation = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
_host = Host.CreateDefaultBuilder()
.ConfigureAppConfiguration(c => c.SetBasePath(appLocation))
.ConfigureServices(ConfigureServices)
.Build();
}
private void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
// Core Services
services.AddSingleton<IFileService, FileService>();
// Services
services.AddSingleton<IThemeSelectorService, ThemeSelectorService>();
services.AddSingleton<ISystemService, SystemService>();
services.AddSingleton<IPersistAndRestoreService, PersistAndRestoreService>();
services.AddSingleton<IApplicationInfoService, ApplicationInfoService>();
services.AddSingleton<IPageService, PageService>();
services.AddSingleton<INavigationService, NavigationService>();
// ViewModels
services.AddTransient<SettingsViewModel>();
services.AddTransient<MainViewModel>();
// Configuration
services.Configure<AppConfig>(context.Configuration.GetSection(nameof(AppConfig)));
}
// TODO WTS: Add tests for functionality you add to SettingsViewModel.
[Fact]
public void TestSettingsViewModelCreation()
{
var vm = _host.Services.GetService(typeof(SettingsViewModel));
Assert.NotNull(vm);
}
[Fact]
public void TestGetSettingsPageType()
{
if (_host.Services.GetService(typeof(IPageService)) is IPageService pageService)
{
var pageType = pageService.GetPageType(typeof(SettingsViewModel).FullName);
Assert.Equal(typeof(SettingsPage), pageType);
}
else
{
Assert.True(false, $"Can't resolve {nameof(IPageService)}");
}
}
// TODO WTS: Add tests for functionality you add to MainViewModel.
[Fact]
public void TestMainViewModelCreation()
{
var vm = _host.Services.GetService(typeof(MainViewModel));
Assert.NotNull(vm);
}
[Fact]
public void TestGetMainPageType()
{
if (_host.Services.GetService(typeof(IPageService)) is IPageService pageService)
{
var pageType = pageService.GetPageType(typeof(MainViewModel).FullName);
Assert.Equal(typeof(MainPage), pageType);
}
else
{
Assert.True(false, $"Can't resolve {nameof(IPageService)}");
}
}
}
}