Add chapter 10 sample project

This commit is contained in:
Alvin Ashcraft
2023-08-13 13:40:04 -04:00
parent f9e4d407b9
commit 90f935f4b2
91 changed files with 4110 additions and 0 deletions
@@ -0,0 +1,23 @@
using Microsoft.Windows.ApplicationModel.DynamicDependency;
[assembly: WinUITestTarget(typeof(TemplateStudioSampleApp.App))]
namespace TemplateStudioSampleApp.Tests.MSTest;
[TestClass]
public class Initialize
{
[AssemblyInitialize]
public static void AssemblyInitialize(TestContext context)
{
// TODO: Initialize the appropriate version of the Windows App SDK.
// This is required when testing MSIX apps that are framework-dependent on the Windows App SDK.
Bootstrap.TryInitialize(0x00010001, out var _);
}
[AssemblyCleanup]
public static void AssemblyCleanup()
{
Bootstrap.Shutdown();
}
}
@@ -0,0 +1,67 @@
*Recommended Markdown Viewer: [Markdown Editor](https://marketplace.visualstudio.com/items?itemName=MadsKristensen.MarkdownEditor2)*
## Getting Started
[Get started with unit testing](https://docs.microsoft.com/visualstudio/test/getting-started-with-unit-testing?view=vs-2022&tabs=dotnet%2Cmstest), [Use the MSTest framework in unit tests](https://docs.microsoft.com/visualstudio/test/using-microsoft-visualstudio-testtools-unittesting-members-in-unit-tests), and [Run unit tests with Test Explorer](https://docs.microsoft.com/visualstudio/test/run-unit-tests-with-test-explorer) provide an overview of the MSTest framework and Test Explorer.
## Testing UI Controls
Unit tests that exercise UI controls must run on the WinUI UI thread or they will throw an exception. To run a test on the WinUI UI thread, mark the test method with `[UITestMethod]` instead of `[TestMethod]`. During test execution, the test host will launch the app and dispatch the test to the app's UI thread.
The below example creates a `new Grid()` and then validates that its `ActualWidth` is `0`.
```csharp
[UITestMethod]
public void UITestMethod()
{
Assert.AreEqual(0, new Grid().ActualWidth);
}
```
## Dependency Injection and Mocking
Template Studio uses [dependency injection](https://docs.microsoft.com/dotnet/core/extensions/dependency-injection) which means class dependencies implement interfaces and those dependencies are injected via class constructors.
One of the many benefits of this approach is improved testability, since tests can produce mock implementations of the interfaces and pass them into the object being tested, isolating the object being tested from its dependencies. To mock an interface, create a class that implements the interface, create stub implementations of the interface members, then pass an instance of the class into the object constructor.
The below example demonstrates testing the ViewModel for the Settings page. `SettingsViewModel` depends on `IThemeSelectorService`, so a `MockThemeSelectorService` class is introduced that implements the interface with stub implementations, and then an instance of that class is passed into the `SettingsViewModel` constructor. The `VerifyVersionDescription` test then validates that the `VersionDescription` property of the `SettingsViewModel` returns the expected value.
```csharp
// SettingsViewModelTests.cs
[TestClass]
public class SettingsViewModelTests
{
private readonly SettingsViewModel _viewModel;
public SettingsViewModelTests()
{
_viewModel = new SettingsViewModel(new MockThemeSelectorService());
}
[TestMethod]
public void VerifyVersionDescription()
{
Assert.IsTrue(Regex.IsMatch(_viewModel.VersionDescription, @"App1 - \d\.\d\.\d\.\d"));
}
}
```
```csharp
// Mocks/MockThemeSelectorService.cs
internal class MockThemeSelectorService : IThemeSelectorService
{
public ElementTheme Theme => ElementTheme.Default;
public Task InitializeAsync() => Task.CompletedTask;
public Task SetRequestedThemeAsync() => Task.CompletedTask;
public Task SetThemeAsync(ElementTheme theme) => Task.CompletedTask;
}
```
## CI Pipelines
See [README.md](https://github.com/microsoft/TemplateStudio/blob/main/docs/WinUI/pipelines/README.md) for guidance on building and testing projects in CI pipelines.
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-windows10.0.19041.0</TargetFramework>
<RootNamespace>TemplateStudioSampleApp.Tests.MSTest</RootNamespace>
<Platforms>x86;x64;arm64</Platforms>
<IsPackable>false</IsPackable>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseWinUI>true</UseWinUI>
<WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained>
<ProjectPriFileName>resources.pri</ProjectPriFileName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TemplateStudioSampleApp\TemplateStudioSampleApp.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,50 @@
using System.Diagnostics;
using Microsoft.UI.Xaml.Controls;
namespace TemplateStudioSampleApp.Tests.MSTest;
// TODO: Write unit tests.
// https://docs.microsoft.com/visualstudio/test/getting-started-with-unit-testing
// https://docs.microsoft.com/visualstudio/test/using-microsoft-visualstudio-testtools-unittesting-members-in-unit-tests
// https://docs.microsoft.com/visualstudio/test/run-unit-tests-with-test-explorer
[TestClass]
public class TestClass
{
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
Debug.WriteLine("ClassInitialize");
}
[ClassCleanup]
public static void ClassCleanup()
{
Debug.WriteLine("ClassCleanup");
}
[TestInitialize]
public void TestInitialize()
{
Debug.WriteLine("TestInitialize");
}
[TestCleanup]
public void TestCleanup()
{
Debug.WriteLine("TestCleanup");
}
[TestMethod]
public void TestMethod()
{
Assert.IsTrue(true);
}
[UITestMethod]
public void UITestMethod()
{
Assert.AreEqual(0, new Grid().ActualWidth);
}
}
@@ -0,0 +1,2 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
global using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;