简体   繁体   English

模拟打开对话框以从驱动器中选择文件名的 openDialog 框

[英]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.

相关问题 从WPF应用程序打开并运行Outlook时如何获取Outlook选择名称对话框的焦点 - How to get outlook select name dialog box to focus when opened from wpf application and outlook is running 从Selenium WebDriver读取Windows File Open对话框中的文件名 - Reading file name in Windows File Open Dialog box from Selenium WebDriver 模拟一个将回调类型作为参数的对话框 - Mocking a dialog box that takes a callback type as argument 在单元测试中嘲笑一个对话框 - mocking a dialog box inside unit tests 调用“选择文件”对话框时出现NullReferenceException错误 - Getting a NullReferenceException Error when invoking a Select File Dialog Box 如何在对话框中显示已移动的文件数和已移动的文件名 - How to show the number of files moved and moved file name in a dialog box 仅从“保存文件”对话框中获取名称 - Get only name from Save File Dialog 从 Web 下载文件,然后使用保存文件对话框保存? - Download file from web and then save with a save file dialog box? 在.net中使用Moq模拟对话框后,在对话框上获取消息 - fetching the Message on the Dialog box after Mocking the Dialog box using Moq in .net 打开文件对话框之前,从图片框中显示默认图片 - Show default picture from a picture box before opening a file dialog
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM