Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

bUnit + xUnit Razor tests failing
ClaytonHunt
#1 Posted : Thursday, February 11, 2021 4:01:47 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 1/28/2013(UTC)
Posts: 2
Location: United States of America

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
I am testing a Blazor application. I have normal C# tests in this project that run just fine, however, when adding this first Razor syntax test I get the following error.

All other tests work, and This test runs in VS Test runner, and in Resharper Test Runner. I have tried to provide enough information for reproduction of the issue. Sorry if it is too much.
I absolutely love NCrunch and have been a user for nearly 10 years at this point. Thank you for all the hard work and I appreciate any help I can get for this issue.

My version of NCrunch is 4.6.0.3 and is running in Visual Studio Pro 2019 Version 16.8.5

Code:

An error occurred while analysing this project after it was built: System.Exception: DiscoverRazorTests: Discovering in MyProject.MyTests.
GetSourceInformation(MyTests): Attempting to find source file
GetSourceInformation(MyTests): Verifying file = C:\{MyPath}\MyTests.razor
GetSourceInformation(MyTests): Source info found: File = C:\{MyPath}\MyTests.razor, LineNumber = 3
Error while discovering test 'MyProject.MyTests.RazorTests':System.NullReferenceException: Object reference not set to an instance of an object.
at nCrunch.Module.XUnit2.Integration.XUnitNCrunchDiscoveredTestContainer.convertTraitsToCategories(Dictionary`2 traits)
at nCrunch.Module.XUnit2.Integration.XUnitNCrunchDiscoveredTestContainer.StoreDiscoveredTest(ITestCase testCase, TestName testName)
at nCrunch.Module.XUnit2.Integration.XUnitDiscoveryMessageSink.discoverTest(ITestCase testCase)
at nCrunch.Module.XUnit2.Integration.XUnit2DiscoveryEnvironment.FindFrameworkTestsInAssembly(ReflectedAssembly assembly, FilePath assemblyFilePath, IList`1 referencedAssemblyFilePaths, ComponentUniqueName testComponentUniqueName, PlatformType platformType, DynamicProxy[] dynamicProxies)
at nCrunch.TestExecution.TestFinder.<>c__DisplayClass0_2.<FindTestsForFrameworks>b__1()
at nCrunch.Common.PerformanceTracking.PerfTracker.TrackActivity(String name, Action activity)
at nCrunch.TestExecution.TestFinder.<>c__DisplayClass0_0.<FindTestsForFrameworks>b__0()
at nCrunch.Common.PerformanceTracking.PerfTracker.TrackActivity(String name, Action activity)
at nCrunch.TestExecution.TestFinder.FindTestsForFrameworks(ReflectedAssembly assembly, FilePath assemblyFilePath, IList`1 referencedAssemblyFilePaths, DescribedTestFrameworkDiscoverer[] describedDiscoverers, ComponentUniqueName testComponentUniqueName, PlatformType platformType, DynamicProxy[] dynamicProxies)
at nCrunch.TestExecution.RemoteTaskRunner.AnalyseAssembly(DescribedTestFrameworkDiscoverer[] applicableFrameworks, ComponentUniqueName testComponentUniqueName, PerfTracker perfTracker)


MyProject.csproj
Code:

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

<PropertyGroup>
<IsPackable>false</IsPackable>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="bunit" Version="1.0.0-preview-01" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />

<PackageReference Include="coverlet.collector" Version="3.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>


_Imports.razor
Code:

@using Xunit
@using Bunit
@using AngleSharp.Dom
@using Bunit.TestDoubles
@using Microsoft.JSInterop
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.Extensions.DependencyInjection


MyTests.razor
Code:

@inherits TestComponentBase

<Fixture Test="NoErrorsDisplaysEmpty">
<ComponentUnderTest>
<h1>Hello World</h1>
</ComponentUnderTest>

@code {
public void NoErrorsDisplaysEmpty(Fixture fixture)
{

}
}
</Fixture>
Remco
#2 Posted : Thursday, February 11, 2021 11:20:01 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Hi, thanks for sharing this issue.

This problem is caused by a compatibility issue between NCrunch and bunit. Basically, bunit changes some of the behaviour around how the test run is handled and the results are reported. Presently, we do not have support for this framework. Adding support would need to go through a proper development process so that we can test the use cases and patch up any issues. You are most welcome to make a feature request for it if you like.
1 user thanked Remco for this useful post.
ClaytonHunt on 2/12/2021(UTC)
egilhansen
#3 Posted : Friday, February 12, 2021 1:06:55 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 2/12/2021(UTC)
Posts: 2
Location: Iceland

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
Remco;15319 wrote:
Hi, thanks for sharing this issue.

This problem is caused by a compatibility issue between NCrunch and bunit. Basically, bunit changes some of the behaviour around how the test run is handled and the results are reported. Presently, we do not have support for this framework. Adding support would need to go through a proper development process so that we can test the use cases and patch up any issues. You are most welcome to make a feature request for it if you like.


Hi @Remco, creator of bUnit here. Can you elaborate on what I might do differently?

My extension to xUnit to enable writing tests in .razor tests follows the pattern recommend by the xUnit framework, where one extends a bunch of xUnit base classses to create a custom test discoverer and test runner.

@ClaytonHunt have you tried to write tests using the "inline render fragment" available in preview 1 of bUnit. I do not have a NCrunch license, so havent tested my self, but the code looks like this (code picked from here):

Code:
@inherits TestContext
@code {
    [Theory, AutoData]
    public void PizzaCard_Correctly_Lists_The_PizzaSpecial_Passed_To_It(PizzaSpecial pizza)
    {
        var cut = Render(@<PizzaCard Special="pizza" />);

        cut.MarkupMatches(@<li style="background-image: url('@pizza.ImageUrl')">
                                <div class="pizza-info">
                                    <span class="title">@pizza.Name</span>
                                    @pizza.Description
                                    <span class="price">@pizza.GetFormattedBasePrice()</span>
                                </div>
                            </li>);
    }
    
    [Theory, AutoData]
    public void PizzaCard_Raises_Clicked_Event_When_Clicked_By_User(PizzaSpecial inputPizza)
    {
        PizzaSpecial? clickedPizza = null;
        var cut = Render(@<PizzaCard Special="inputPizza" OnClick="pizza => clickedPizza = pizza" />);     

        cut.Find("li").Click();

        clickedPizza.Should().Be(inputPizza);
    }
}


It would be interesting to know if this works with NCrunch.

NOTE: Using AutoFixture to generate test data (thats the AutoData attribute).

Best regards, Egil
1 user thanked egilhansen for this useful post.
ClaytonHunt on 2/12/2021(UTC)
ClaytonHunt
#4 Posted : Friday, February 12, 2021 1:32:59 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 1/28/2013(UTC)
Posts: 2
Location: United States of America

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
Excellent work around! @Egilhansen Initial results for that format of a test are working fine. It must be something in the <Fixture> component that is blowing up. I am unfamiliar with AutoFixture I will have to look into that.
1 user thanked ClaytonHunt for this useful post.
egilhansen on 2/12/2021(UTC)
egilhansen
#5 Posted : Friday, February 12, 2021 1:52:28 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 2/12/2021(UTC)
Posts: 2
Location: Iceland

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
ClaytonHunt;15321 wrote:
Excellent work around! @Egilhansen Initial results for that format of a test are working fine. It must be something in the <Fixture> component that is blowing up. I am unfamiliar with AutoFixture I will have to look into that.


I am using AutoFixture and this style of bUnit tests on stream these days. Talk about AutoFixture in details during the first episode if you are curious.
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.048 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download