문제

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