ninjagast
ninjagast

Reputation: 41

Reqnroll BDD tests not working with a Blazor Maui Hybrid app

I created a new Maui Blazor app with VS 2022. I have the Reqnroll plugin installed and created a new project within the solution. I added a project reference to the first Maui Blazor app just like the documentation states. (Reqnroll documentation)

I changed the TargetFrameworks in both projectfiles to "net8.0-windows10.0.19041.0". I build both projects after making a dummy test which works as expected. (Both application also run without any issues) But when I run "dotnet test" in the terminal I get the following error:

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Given The class has been instantiated
-> error: The type initializer for '<Module>' threw an exception. (0,1s)
When The function is called
-> skipped because of previous errors
Then it should return true
-> skipped because of previous errors

  Failed CallingTestFunction [231 ms]
  Error Message:
   System.TypeInitializationException : The type initializer for '<Module>' threw an exception.
  ----> System.TypeInitializationException : The type initializer for 'WinRT.ActivationFactory`1' threw an exception.
  ----> System.Runtime.InteropServices.COMException : Class not registered (0x80040154 (REGDB_E_CLASSNOTREG))
  Stack Trace:
   ...

Here are my project files:

Maui Blazor

<Project Sdk="Microsoft.NET.Sdk.Razor">

    <PropertyGroup>
        <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>

        <OutputType>Exe</OutputType>
        <RootNamespace>TestForReqnroll</RootNamespace>
        <UseMaui>true</UseMaui>
        <SingleProject>true</SingleProject>
        <ImplicitUsings>enable</ImplicitUsings>
        <EnableDefaultCssItems>false</EnableDefaultCssItems>
        <Nullable>enable</Nullable>

        <!-- Display name -->
        <ApplicationTitle>TestForReqnroll</ApplicationTitle>

        <!-- App Identifier -->
        <ApplicationId>com.companyname.testforreqnroll</ApplicationId>

        <!-- Versions -->
        <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
        <ApplicationVersion>1</ApplicationVersion>

        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
        <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
        <Platforms>x64</Platforms>
    </PropertyGroup>

    <ItemGroup>
        <!-- App Icon -->
        <MauiIcon Update="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />

        <!-- Splash Screen -->
        <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />

        <!-- Images -->
        <MauiImage Include="Resources\Images\*" />
        <MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />

        <!-- Custom Fonts -->
        <MauiFont Include="Resources\Fonts\*" />

        <!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
        <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
        <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
        <PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="$(MauiVersion)" />
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
    </ItemGroup>

</Project>

The test project

<Project Sdk="Microsoft.NET.Sdk.Razor">
    <PropertyGroup>
      <TargetFrameworks>net8.0-windows10.0.19041.0</TargetFrameworks>
      <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <Platforms>x64</Platforms>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
    <PackageReference Include="Reqnroll.NUnit" Version="1.0.0" />
    <PackageReference Include="nunit" Version="3.14.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
    <PackageReference Include="FluentAssertions" Version="6.12.0" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Drivers\" />
    <Folder Include="Support\" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\TestForReqnroll\TestForReqnroll.csproj" />
  </ItemGroup>

</Project>

Test step definition

using NUnit.Framework;
using TestForReqnroll.Components;

namespace TestForReqnroll.test.StepDefinitions
{
    [Binding]
    public class TestStepDefinitions
    {
        private testClass testClass;

        [Given("The class has been instantiated")]
        public void GivenTheClassHasBeenInstantiated()
        {
            testClass = new testClass();
        }

        [When("The function is called")]
        public void WhenTheFunctionIsCalled()
        {
            testClass.testFunction();
        }

        [Then("it should return true")]
        public void ThenItShouldReturnTrue()
        {
            Assert.AreEqual(true, true);
        }
    }
}

Screenshot of Entire Solution

and a screenshot of my entire solution

Upvotes: 2

Views: 187

Answers (1)

ninjagast
ninjagast

Reputation: 41

I found a work around. You should keep the net8.0 in the reqnroll project file. And create a new targetframework in the Maui Blazor project file for net8.0. After this you'll need to output the project as a DLL instead of the standard exe. The working project files can be found below.

Maui Blazor project

<Project Sdk="Microsoft.NET.Sdk.Razor">

    <PropertyGroup>
        <!-- Added the net8.0 to the targetframeworks -->
        <TargetFrameworks>net8.0-ios;net8.0-maccatalyst;net8.0-android34.0;net8.0</TargetFrameworks>
        <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>

        <!-- also added this condition to the outputtype. So it defaults to dll when we build the app in net8.0 -->
        <OutputType Condition="'$(TargetFramework)' != 'net8.0'">Exe</OutputType>
        <RootNamespace>TestForReqnroll</RootNamespace>
        <UseMaui>true</UseMaui>
        <SingleProject>true</SingleProject>
        <ImplicitUsings>enable</ImplicitUsings>
        <EnableDefaultCssItems>false</EnableDefaultCssItems>
        <Nullable>enable</Nullable>

        <!-- Display name -->
        <ApplicationTitle>TestForReqnroll</ApplicationTitle>

        <!-- App Identifier -->
        <ApplicationId>com.companyname.testforreqnroll</ApplicationId>

        <!-- Versions -->
        <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
        <ApplicationVersion>1</ApplicationVersion>

        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
        <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
        <Platforms>x64</Platforms>
    </PropertyGroup>

    <ItemGroup>
        <!-- App Icon -->
        <MauiIcon Update="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />

        <!-- Splash Screen -->
        <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />

        <!-- Images -->
        <MauiImage Include="Resources\Images\*" />
        <MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />

        <!-- Custom Fonts -->
        <MauiFont Include="Resources\Fonts\*" />

        <!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
        <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
        <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
        <PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="$(MauiVersion)" />
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
    </ItemGroup>

</Project>

Reqnroll test project

<Project Sdk="Microsoft.NET.Sdk.Razor">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseMaui>true</UseMaui>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
    <PackageReference Include="Reqnroll.NUnit" Version="1.0.0" />
    <PackageReference Include="nunit" Version="3.14.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
    <PackageReference Include="FluentAssertions" Version="6.12.0" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Drivers\" />
    <Folder Include="Support\" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\TestForReqnroll\TestForReqnroll.csproj">
       <SetTargetFramework>TargetFramework=net8.0</SetTargetFramework>
    </ProjectReference>
  </ItemGroup>

</Project>

Upvotes: 2

Related Questions