I'm following this doc to unit test a WinUi 3 project using MSTest.

https://devblogs.microsoft.com/ifdef-windows/winui-desktop-unit-tests/

I had this build error after edited OnLaunched method from App.xaml.cs file:

error CS7069: Reference to type 'DispatcherQueue' claims it is defined in 'Microsoft.WinUI', but it could not be found.

App.xaml.cs:

using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
...
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
    Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.CreateDefaultUI();

    m_window = new MainWindow();

    // Ensure the current window is active
    m_window.Activate();

    UITestMethodAttribute.DispatcherQueue = m_window.DispatcherQueue; //Error

    // Replace back with e.Arguments when https://github.com/microsoft/microsoft-ui-xaml/issues/3368 is fixed
    Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.Run(Environment.CommandLine);
}

Here is issue about the same error. https://github.com/microsoft/testfx/issues/993

What does this solution mean? I still don't know how to resolve this error.

DispatcherQueue is moved to Microsoft.UI.Dispatching namespace. You might want to check your references.

Environment:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <RootNamespace>WelcomeScreen</RootNamespace>
    <ApplicationManifest>app.manifest</ApplicationManifest>
   
    <Platforms>x86;x64;ARM64</Platforms>
    <RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
    <PublishProfile>win10-$(Platform).pubxml</PublishProfile>
    <UseWinUI>true</UseWinUI>
    <EnableMsixTooling>true</EnableMsixTooling>
  </PropertyGroup>
  <ItemGroup>
    <AppxManifest Include="Package.appxmanifest">
      <SubType>Designer</SubType>
    </AppxManifest>
  </ItemGroup>
  <ItemGroup>
    <ProjectCapability Include="TestContainer" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.230913002" />
    <PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.755" />
    <PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.9" />
    <Manifest Include="$(ApplicationManifest)" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="MSTest.TestAdapter">
      <Version>3.2.2</Version>
    </PackageReference>
    <PackageReference Include="MSTest.TestFramework">
      <Version>2.2.4</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.TestPlatform.TestHost">
      <Version>17.10.0-preview-24080-01</Version>
      <ExcludeAssets>build</ExcludeAssets>
    </PackageReference>
  </ItemGroup>
1

There are 1 best solutions below

0
Andrew KeepCoding On BEST ANSWER

It seems that there's been some break changes since that blog was published. Updating the NuGet packages to the latest version should fix that compile error.

<ItemGroup>
  <PackageReference Include="MSTest.TestAdapter">
    <Version>3.2.2</Version>
  </PackageReference>
  <PackageReference Include="MSTest.TestFramework">
    <Version>3.2.2</Version>
  </PackageReference>
  <PackageReference Include="Microsoft.TestPlatform.TestHost">
    <Version>17.9.0</Version>
    <ExcludeAssets>build</ExcludeAssets>
  </PackageReference>
</ItemGroup>