[英]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.我正在使用
moq
进行模拟,我能够成功地模拟这个,但是在此之后我的下一个语句是调用一个方法,该方法将在此对话框中输入的文件名作为参数。 So how do I mock this dialog box in Unit Test cases so as to get the filename
inside the called method?那么如何在单元测试用例中模拟这个对话框,以便在被调用的方法中获取
filename
? I mean how to pass filename
inside the Unit Test case so that it will get inserted to the next method call?我的意思是如何在单元测试用例中传递
filename
,以便将其插入到下一个方法调用中?
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:假设您对传递给
OpenDialog
方法的参数不太挑剔,您可以像这样设置模拟服务方法:
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.它会将硬编码的文件名
"SomeFakeFileName.ext"
返回给您的 SUT。
Similarly, if your service also offers a stateful "last FileName" property:同样,如果您的服务还提供有状态的“last FileName”属性:
mockUIService
.SetupGet(_ => _.FileName)
.Returns("SomeFakeFileName.ext");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.