0

Been trying to unit test the component rendered inside Blazored Modal. I can of course test the code around it by mocking IModalService but if I want to test the modal component itself, I run into problems. And they stem from calls to BlazoredModalInstance, like to BlazoredModalInstance.CloseAsync(ModalResult).

I would have liked to mock it but since it doesn't implement any interface, I can't. I tried to create a new BlazoredModal component, but it takes the BlazoredModalInstance as a private CascadingParameter which I cannot provide.

I have also tried rendering the BlazoredModalInstance with bUnit, giving it some ModalOptions, and passing the instance to my modal component, but calls to it lead to crashes (null reference).

Is this somehow possible to achieve? Is there a way to provide cascading parameters without the parameters.Add(...) way?

SamiHuutoniemi
  • 1,576
  • 2
  • 16
  • 35
  • 1
    Try taking a look at BlazoredModel's own tests, they might have the solution: https://github.com/Blazored/Modal/blob/main/tests/src/Blazored.Modal.Tests/DisplayTests.cs – Egil Hansen May 11 '22 at 13:12
  • @EgilHansen Yes I did that also, but they look like: `var cut = RenderComponent(CascadingValue(modalService));`. I can't say i have seen that CascadingValue-method anywhere... – SamiHuutoniemi May 12 '22 at 07:05
  • It's an old style of passing parameters that is not encourage any more. Check out the docs for how to do it today: https://bunit.dev/docs/providing-input/passing-parameters-to-components.html#cascading-parameters-and-cascading-values – Egil Hansen May 13 '22 at 08:30
  • @EgilHansen This will not work since the cascading parameter is private... :( – SamiHuutoniemi May 16 '22 at 10:09
  • https://stackoverflow.com/questions/68531809/how-to-open-a-blazored-modal-with-bunit/68534950#68534950 – Egil Hansen May 17 '22 at 09:00
  • @EgilHansen I read that as well. And like you say there, I do test that the modal is invoked, and the response from it by mocking IModalService. But that doesn't actually touch the modal component itself at all. And if I want to test the component inside the modal, I get the problem with no BlazoredModalInstance... – SamiHuutoniemi May 17 '22 at 12:31

0 Answers0