Skip to content

Value cannot be null, Parameter 'jsObjectReference' when using AspNetCore Title component #311

Closed

Description

@dawidr

I've a problem writing test for a page which is using Microsoft.AspNetCore.Components.Web.Extensions.Head.Title component.
No issues when I remove the Title. The page looks like this:

@page "/sample"

<Title Value="Sample"></Title>

<div>@Text</div>

@code {
    [Parameter]
    public string Text { get; set; } = string.Empty;
}

And here is the test:

using Bunit;
using Bunit.TestDoubles;
using Xunit;

namespace Tests
{
    public class SampleTests
    {
        private TestContext _context;

        public SampleTests()
        {
            _context = new TestContext();
            _context.Services.AddMockJSRuntime();
        }

        [Fact]
        public void PageShouldRender()
        {
            var html = _context.RenderComponent<Sample>(parameters =>
                parameters.Add(p => p.Text, "Sample"));
        }

    }
}

Error:
{"Value cannot be null. (Parameter 'jsObjectReference')"}

image

Activity

  1. egil commented on Feb 1, 2021

    @egil
    Member

    Hi @dawidr

    Please provide the following info:

    • bUnit library version:
    • .NET Core/Blazor version:
  2. dawidr commented on Feb 3, 2021

    @dawidr
    Author

    @egil After your comment, I noticed that I've outdated bUnit. So right now I'm on bUnit 1.0.0-preview-01 and .Net Core 5.0.1.
    I removed line _context.Services.AddMockJSRuntime(); as it is obsolete.

    The error is different right now:

    Bunit.JSRuntimeUnhandledInvocationException: 'The invocation of 'import' with the argument [./_content/Microsoft.AspNetCore.Components.Web.Extensions/headManager.js] was not expected.'

    To put more light on the <Title> component. It requires <script src="_content/Microsoft.AspNetCore.Components.Web.Extensions/headManager.js"></script> in the layout file.

  3. egil commented on Feb 3, 2021

    @egil
    Member

    Try settting JSRuntimeMode to Loose, see here for more details: https://bunit.egilhansen.com/docs/test-doubles/emulating-ijsruntime.html

  4. locked and limited conversation to collaborators on Feb 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions