Mocking the openDialog box that opens the dialog to select file name from drive

StackOverflow https://stackoverflow.com/questions/23132691

  •  05-07-2023
  •  | 
  •  

سؤال

I am unit Testing the dialog boxes.

Here is code:

string fileName = this.uiService.OpenDialog(
    strExtensions, 
    this.resourceManager.GetResourceString(
                   StudioResourceManagerName.StudioResourceManager, "IDC_Open"),
    strInitialLocation);

I am using moq for mocking and I am able to successfully mock this but my next statement after this is calling a method that takes the filename entered in this Dialog box as argument. So how do I mock this dialog box in Unit Test cases so as to get the filename inside the called method? I mean how to pass filename inside the Unit Test case so that it will get inserted to the next method call?

هل كانت مفيدة؟

المحلول

Assuming that you aren't too picky about what parameters are passed to the OpenDialog method , you can setup the mock service method like so:

mockUIService
   .Setup(_ => _.OpenDialog(It.IsAny<String>(), 
                            It.IsAny<String>(), 
                            It.IsAny<String>())
   .Returns("SomeFakeFileName.ext");

It will return the hardcoded filename "SomeFakeFileName.ext" to your SUT.

Similarly, if your service also offers a stateful "last FileName" property:

mockUIService
   .SetupGet(_ => _.FileName) 
   .Returns("SomeFakeFileName.ext");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top