Question

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?

Was it helpful?

Solution

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");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top