bUnit-dev / bUnit

bUnit is a testing library for Blazor components that make tests look, feel, and runs like regular unit tests. bUnit makes it easy to render and control a component under test’s life-cycle, pass parameter and inject services into it, trigger event handlers, and verify the rendered markup from the component using a built-in semantic HTML comparer.
https://bunit.dev
MIT License
1.13k stars 105 forks source link

Tests throw exception 'File not found' #221

Closed bfmsoft closed 4 years ago

bfmsoft commented 4 years ago

Just try to run tests and get "File not found". This is testing my component that basically processing enums to strings. This worked up till RC1 and beta 10. Did I do something wrong? I saw a note in RC1 that talks here that talks about binary issues but not source. Does this have something to do with it?

**Full error: Test Name: BfmSoft.Web.BlazorUtil.Tests.RazorTestComponents.BfmEnumInputSelectTest.RazorTests Test FullName: BfmSoft.Web.BlazorUtil.Tests.BfmSoft.Web.BlazorUtil.Tests.RazorTestComponents.BfmEnumInputSelectTest.BfmSoft.Web.BlazorUtil.Tests.RazorTestComponents.BfmEnumInputSelectTest.RazorTests Test Source: : line -1 Test Outcome: Failed Test Duration: 0:00:00

Test Name: BfmSoft.Web.BlazorUtil.Tests.RazorTestComponents.BfmEnumInputSelectTest.RazorTests Test Outcome: Failed Result Message: System.InvalidOperationException : Exception thrown during razor test discovery on 'BfmSoft.Web.BlazorUtil.Tests.RazorTestComponents.BfmEnumInputSelectTest'. Field not found: 'Microsoft.AspNetCore.Components.RenderTree.RenderTreeFrame.FrameType'.

Example:

@inherits TestComponentBase
@using BfmSoft.Web.BlazorUtil.Components

@code {
    private MockJSRuntimeInvokeHandler MockJsRuntime { get; set; } = default!;

    private Register Register { get; set; } = new Register();
    private EditContext EditContext { get; set; } = default!;

    private IInputSelectUtil InputSelectUtil { get; set; } = default!;

    private void Setup(Fixture fixture)
    {
        MockJsRuntime = fixture.Services.AddMockJSRuntime();
        EditContext = new EditContext(Register);

        Register.UserCountry = SecurePileEnums.Country.UnitedStates;
    }
}

<Fixture Setup="Setup" Test="BfmEnumInputSelectNoInitTest">
    <ComponentUnderTest>
        <EditForm Model="Register">
            <BfmEnumInputSelect Id="CountryId"
                                InputSelectUtil="@InputSelectUtil"
                                EnumTypeId="SecurePileEnums.EnumCountry"
                                @bind-Item="@Register.UserCountry"
                                ValidationFor="() => Register.UserCountry" />
        </EditForm>
    </ComponentUnderTest>
    <Fragment>
        <form>
            <div class="form-control-wrapper">
                <div>Loading, please wait...</div>
            </div>
        </form>
    </Fragment>

    @code {
        public void BfmEnumInputSelectNoInitTest(Fixture fixture)
        {
            InputSelectUtil = new InputSelectUtil();

            var cut = fixture.GetComponentUnderTest();

            cut.MarkupMatches(fixture.GetFragment());
        }
    }
</Fixture>

<Fixture Setup="Setup" Test="BfmEnumInputSelectDefaultTest">
    <ComponentUnderTest>
        <EditForm Model="Register">
            <BfmEnumInputSelect Id="CountryId"
                                InputSelectUtil="@InputSelectUtil"
                                EnumTypeId="SecurePileEnums.EnumCountry"
                                @bind-Item="@Register.UserCountry"
                                ValidationFor="() => Register.UserCountry" />
        </EditForm>
    </ComponentUnderTest>
    <Fragment>
        <form>
            <div class="form-control-wrapper">
                <select class="form-control " id="CountryId" onchange="OnChange">
                    <option value="128" selected="">- Select Item -</option>
                    <option value="129">United States</option>
                </select>
            </div>
        </form>
    </Fragment>

    @code {
        private void BfmEnumInputSelectDefaultTest(Fixture fixture)
        {
            var inputSelectUtil = new Moq.Mock<IInputSelectUtil>();
            inputSelectUtil.Setup(e => e.GetEnums<SecurePileEnums.Country>(SecurePileEnums.EnumCountry)).Returns(
                    new (int enumId, string enumText)[] {
                        ((int)SecurePileEnums.Country.Default, "- Select Item -"),
                        ((int)SecurePileEnums.Country.UnitedStates, "United States"),
                    }
                );
            InputSelectUtil = inputSelectUtil.Object;

            Register.UserCountry = SecurePileEnums.Country.Default;
            var cut = fixture.GetComponentUnderTest();
            cut.MarkupMatches(fixture.GetFragment());
        }
    }
</Fixture>

<Fixture Setup="Setup" Test="BfmEnumInputSelectUnitedStatesTest">
    <ComponentUnderTest>
        <EditForm Model="Register">
            <BfmEnumInputSelect Id="CountryId"
                                InputSelectUtil="@InputSelectUtil"
                                EnumTypeId="SecurePileEnums.EnumCountry"
                                @bind-Item="@Register.UserCountry"
                                ValidationFor="() => Register.UserCountry" />
        </EditForm>
    </ComponentUnderTest>
    <Fragment>
        <form>
            <div class="form-control-wrapper">
                <select class="form-control " id="CountryId" onchange="OnChange">
                    <option value="128">- Select Item -</option>
                    <option value="129" selected="">United States</option>
                </select>
            </div>
        </form>
    </Fragment>

    @code {
        private void BfmEnumInputSelectUnitedStatesTest(Fixture fixture)
        {
            var inputSelectUtil = new Moq.Mock<IInputSelectUtil>();
            inputSelectUtil.Setup(e => e.GetEnums<SecurePileEnums.Country>(SecurePileEnums.EnumCountry)).Returns(
                    new (int enumId, string enumText)[] {
                        ((int)SecurePileEnums.Country.Default, "- Select Item -"),
                        ((int)SecurePileEnums.Country.UnitedStates, "United States"),
                    }
                );
            InputSelectUtil = inputSelectUtil.Object;

            Register.UserCountry = SecurePileEnums.Country.UnitedStates;
            var cut = fixture.GetComponentUnderTest();
            cut.MarkupMatches(fixture.GetFragment());
        }
    }
</Fixture>

With this test:

Unit test in C# or Razor or Snapshot that fails.

Results in this output:

Test Name:  BfmSoft.Web.BlazorUtil.Tests.RazorTestComponents.BfmEnumInputSelectTest.RazorTests
Test FullName:  BfmSoft.Web.BlazorUtil.Tests.BfmSoft.Web.BlazorUtil.Tests.RazorTestComponents.BfmEnumInputSelectTest.BfmSoft.Web.BlazorUtil.Tests.RazorTestComponents.BfmEnumInputSelectTest.RazorTests
Test Source:     : line -1
Test Outcome:   Failed
Test Duration:  0:00:00

Test Name:  BfmSoft.Web.BlazorUtil.Tests.RazorTestComponents.BfmEnumInputSelectTest.RazorTests
Test Outcome:   Failed
Result Message: 
System.InvalidOperationException : Exception thrown during razor test discovery on 'BfmSoft.Web.BlazorUtil.Tests.RazorTestComponents.BfmEnumInputSelectTest'.
Field not found: 'Microsoft.AspNetCore.Components.RenderTree.RenderTreeFrame.FrameType'.

Expected behavior: Test should pass (it did before RC1 and beta 10)

Version info:

egil commented 4 years ago

@bfmsoft I just created a quick sample project locally and cannot seem to reproduce. It is attached here, please download it see if you can get it to fail on your machine.

Rc1Test.zip

bfmsoft commented 4 years ago

@egil ok that works. So looked and I had bunit package. I uninstalled and installed. bunit.web and bunit.xunit and it works.

bfmsoft commented 4 years ago

So the issue was I was using bunit package.

egil commented 4 years ago

hmm that should not be an issue. let me investigate a bit.

bfmsoft commented 4 years ago

Plus I am still having the issue that Visual Studio thinks this new version is newer because it does text sorting: v1.0.0-beta-9 SORTS AFTER v1.0.0-beta-10 because of the 1

So If I don't pay attention it will install the old package

egil commented 4 years ago

Hmm nop, still works fine on my machine.

If I change the csproj to this, it still builds and runs fine:

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

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

  <ItemGroup>
    <PackageReference Include="bunit" Version="1.0.0-beta-10" />
    <PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.0.0-rc.1.20451.17" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

</Project>
bfmsoft commented 4 years ago

bunit is only beta9 on my version. That is the problem. The other ones have beta10. With the previous sorting issue.

egil commented 4 years ago

Perhaps this is messing something up for you: https://twitter.com/egilhansen/status/1305992010508185606

bfmsoft commented 4 years ago

Ah Azure Devops is causing it. Because of the sorting issue won't update the in its version. Keeps beta9 because it thinks it is newer. That is my issue.

bfmsoft commented 4 years ago

I have to point directly to nuget.org and not the Devops server to update this package.

egil commented 4 years ago

ok, yeah, sorry about the messed up version numbers. Ill close the issue then.