The invocation of 'render' with the argument [Microsoft.AspNetCore.Components.ElementReference] was not expected. #235
-
We tried to pass ElementReference obj in MockJSRuntime to execute the test case but we are facing method invocation issue related to ElementReference obj. The following code example has used as a component for our application. element property has set as a parameter in js invoke async calls used in OnAfterRenderAsync() method.
The following code example has used as bUnit test case to run the test. While rendering the component, we got that exception and unable to run the test case.
We are facing the below issue in bunit test case. Please find the sample here. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @Naganathan I have a few comments on your sample:
The complete test file look like this: using SampleBunitTest.Pages;
using Bunit;
using Bunit.Rendering;
using Bunit.TestDoubles.JSInterop;
using Microsoft.AspNetCore.Components;
using NUnit.Framework;
using System;
using System.Threading.Tasks;
namespace BlazorBunitTest
{
public abstract class BunitTestContext : ITestContext, IDisposable
{
private Bunit.TestContext _context;
public ITestRenderer Renderer => _context?.Renderer ?? throw new InvalidOperationException("NUnit has not started executing tests yet");
public TestServiceProvider Services => _context?.Services ?? throw new InvalidOperationException("NUnit has not started executing tests yet");
public void Dispose()
{
_context?.Dispose();
_context = null;
}
[SetUp]
public void Setup() => _context = new Bunit.TestContext();
[TearDown]
public void TearDown() => Dispose();
public IRenderedComponent<TComponent> RenderComponent<TComponent>(params ComponentParameter[] parameters) where TComponent : IComponent
=> _context?.RenderComponent<TComponent>(parameters) ?? throw new InvalidOperationException("NUnit has not started executing tests yet");
public IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterBuilder<TComponent>> parameterBuilder) where TComponent : IComponent
=> _context?.RenderComponent<TComponent>(parameterBuilder) ?? throw new InvalidOperationException("NUnit has not started executing tests yet");
}
public class BlazorBarcodeImplicitContextTest : BunitTestContext
{
[Test]
public void SamplebUnit_Does_Not_Renders_Rect_When_SzObj_isRendering_Is_False()
{
// Add the mock js runtime to the test context
var jsMock = Services.AddMockJSRuntime(JSRuntimeMockMode.Strict);
jsMock.Setup<bool>("render", _ => true).SetResult(true);
// Render the component under test
var cut = RenderComponent<SampleBunit>();
// Finds the <rect> element inside the <svg> element
var rectElms = cut.FindAll("svg");
Assert.AreEqual(0, rectElms.Count);
}
}
} Obviously, you move the Hope this helps. |
Beta Was this translation helpful? Give feedback.
Hey @Naganathan
I have a few comments on your sample:
await jsRuntime.InvokeAsync<bool>("render", element);