You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi
I have a test where I'm mocking a razor component on a page.
The component has an EventCallback that updates a private field on the parent page.
This works fine when running the application.
However, I can't get it to work in the test. In the test, I create a mock of the child component and then invoke the callback on the mock. This doesn't seem to update the field on the parent.
[TestClass]publicclassExampleTest:Bunit.TestContext{[TestMethod]publicasyncTaskUpdate_Value_On_Parent_By_Invoking_Callback(){// ARRANGEvarchildComponentMock=newMock<ChildComponent>();ComponentFactories.Add(childComponentMock.Object);varnewValue=100;// ACTvarcut=RenderComponent<Parent>();awaitchildComponentMock.Object.OnUpdate.InvokeAsync(newValue);// ASSERTcut.Find("h2").MarkupMatches($"<h2>Value is: {newValue}</h2>");}}
The test fails with the following output
Actual HTML:
<h2>Value is: 1</h2>
Expected HTML:
<h2>Value is: 100</h2>
I found this issue #1186 which describes a similar approach, but it uses ComponentFactories.AddStub<T>(); instead of ComponentFactories.Add(someObject);, which I need.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi
I have a test where I'm mocking a razor component on a page.
The component has an EventCallback that updates a private field on the parent page.
This works fine when running the application.
However, I can't get it to work in the test. In the test, I create a mock of the child component and then invoke the callback on the mock. This doesn't seem to update the field on the parent.
Here is minimal example
"Parent.razor"
"Parent.razor.cs"
"ChildComponent.razor"
"ChildComponent.razor.cs"
"Test.cs"
The test fails with the following output
I found this issue #1186 which describes a similar approach, but it uses
ComponentFactories.AddStub<T>();
instead ofComponentFactories.Add(someObject);
, which I need.Can you help me?
Beta Was this translation helpful? Give feedback.
All reactions