priya
priya

Reputation: 858

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

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?

Upvotes: 1

Views: 1151

Answers (1)

StuartLC
StuartLC

Reputation: 107247

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");

Upvotes: 2

Related Questions