Any way to mock SfDialogService for testing?

I'm trying to unit test code that calls SfDialogService.ConfirmAsync() but finding it impossible to do so. SfDialogService doesn't implement any interface and has methods that can't be overridden.

I tried creating my own wrapper class, which is injected into my razor component. I can then get my tests to work, but it seems that SfDialogProvider wants just SfDialogService so this breaks the code.

Having a ISfDialogService interface would be a standard way of making this testable.


9 Replies

YS Yohapuja Selvakumaran Syncfusion Team July 2, 2024 12:52 PM UTC

Hi Fred Murphy,


Thank you for reaching out to us. Based on the information provided, we understand that you are looking to display the dialog using the confirm async method. We have a component called Predefined Dialog that allows you to achieve this.


We have created a sample for your reference. Please check it out:




For more information about the Predefined Dialog, you can refer to the documentation and demo below:


Documentation:  https://blazor.syncfusion.com/documentation/predefined-dialogs/getting-started


Demo:  https://blazor.syncfusion.com/demos/predefined-dialogs/default-functionalities?theme=fluent2



Regards,

Yohapuja S


Attachment: Predefined_Dialog_26b90999.zip


FM Fred Murphy July 2, 2024 01:23 PM UTC

No, that's not the issue. I am already using the Predefined Dialog, but need to unit test that my code calls it.

Because the code requires a concrete class (SfDialogService) to be injected, it cannot be mocked. If SfDialogService implemented an interface such as ISfDialogService and was injected with

@inject ISfDialogService DialogService

then it would be possible to mock the service and unit test my code. This is standard practice when using dependency injection, and it seems strange that this practice has not been followed.



YS Yohapuja Selvakumaran Syncfusion Team September 4, 2024 04:59 AM UTC

Hi Fred Murphy,


Thank you for your patience, and we sincerely apologize for the delay in getting back to you.


We understand your concern regarding the inability to mock the SfDialogService due to the lack of an interface implementation, which is indeed a standard practice in dependency injection.


To address your requirement, we have prepared a sample using the dialog service to demonstrate how you might work around this limitation. Please find the attached sample for your reference.


If you have any further questions or need additional assistance, please don't hesitate to reach out. We're here to support you and ensure your success with our components.



Regards,

Yohapuja S


Attachment: Blazor_Dialog_Service_d9038513.zip


FM Fred Murphy September 5, 2024 07:06 AM UTC

Your reply sounds like it has been generated by ChatGPT and the latest sample code does nothing to address the issue.


I have updated my code to bypass the SfDialogService when unit testing. It's not ideal but gets round these limitations.



YS Yohapuja Selvakumaran Syncfusion Team November 19, 2024 01:38 PM UTC

Hi Fred Murphy,


We apologize for the delay in our response and appreciate your patience.


To address your query, we have created a BUnit test case to verify if the dialog is draggable. Below is the code snippet for your reference:


Code Snippet:


 

    //Automation for the Dragable sample attribute

    test('Dragable-Attribute' + theme[index], async ({ page }) => {

        await page.goto(baseUrl + `/dialog/draggable?theme=${theme[index]}`)

        await page.waitForTimeout(3000);

        await expect(page.locator("//*[@class='e-dialog e-lib e-dlg-modal e-draggable e-popup e-control e-popup-open']")).toHaveAttribute('Class', 'e-dialog e-lib e-dlg-modal e-draggable e-popup e-control e-popup-open');

        await expect(page.locator("//*[@class='e-dialog e-lib e-dlg-modal e-draggable e-popup e-control e-popup-open']")).toHaveAttribute('style', 'max-height: 480px; z-index: 10000024; width: 300px; left: 0px; top: 0px; position: relative;');

    });

 


If your requirement differs from this, could you please provide more specific details about the test cases you need to create? This information will help us better understand your needs and provide the appropriate assistance.


Thank you for your patience and cooperation. We look forward to your response.



Regards,

Yohapuja S




RC Robert Chitoiu July 17, 2025 10:26 PM UTC

What horrible answers, definitely ChatGpt or someone who doesn't care about their job.

Anyways, assuming you're using bUnit, this worked for me: 

  TestContext.Services.AddSingleton<SfDialogService>();




FM Fred Murphy July 18, 2025 07:33 AM UTC

Thanks for your reply. Far more helpful than the Syncfusionbots.

I solved my problem by stopping using Syncfusion. Most of their component seem to have been written just to work for a demo and really don't fit real world scenarios.



FM Fred Murphy July 18, 2025 07:34 AM UTC

In case you're wondering, I moved to MudBlazor and find it's much better (and free).



SS Shereen Shajahan Syncfusion Team July 18, 2025 10:53 AM UTC

Hi Robert & Fred,

We wish to clarify that our team remains committed to providing the highest quality of support in addressing customer inquiries and concerns. Please note that we do not rely on ChatGPT responses to resolve such matters. We sincerely regret your dissatisfaction and are saddened by your decision to discontinue using Syncfusion products.

Regards,

Shereen



Loader.
Up arrow icon