模拟一个以回调类型为参数的对话框

时间:2014-03-13 09:05:18

标签: c# unit-testing moq viewmodel

我已经为类型为

的对话框创建了模拟设置
void ShowDialog(string windowName, 
                string parentWindowName,
                Dictionary<string, object> inputFields,
                Action<Dictionary<string, object>> closeCallBack,
                Dictionary<string, object> windowProperties = null);

像:

UIServicemock.Setup(u => u.ShowDialog(It.IsAny<string>(),
                    It.IsAny<string>(),
                    It.IsAny<Dictionary<string, object>>(),
                    It.IsAny<Action<Dictionary<string, object>>>(),
                    It.IsAny<Dictionary<string, object>>())).Callback(ViewName.UnlockScriptPassswordDialog,
                                                                      StudioViewName.MainWindow, 
                                                                      passwordDictionary, ,
                                                                      null);

现在在倒数第二个参数中,我不知道如何传递参数,以便对话框可以调用另一个方法。

我的函数调用是这样的:

uiService.ShowDialog(ViewName.UnlockScriptPassswordDialog,
                     StudioViewName.MainWindow,
                     passwordDictionary,
                     this.OnUnlockScriptSetCallBack, null);

这是调用OnUnlockScriptSetCallBack方法。

1 个答案:

答案 0 :(得分:2)

如果我理解你,你希望调用ShowDialog来调用该方法的第四个参数。设置如下:

UIServicemock.Setup(
                    u =>
                    u.ShowDialog(It.IsAny<string>(), 
                                 It.IsAny<string>(), 
                                 It.IsAny<Dictionary<string, object>>(), 
                                 It.IsAny<Action<Dictionary<string, object>>>(), 
                                 It.IsAny<Dictionary<string, object>>()))
             .Callback<string, 
                       string, 
                       Dictionary<string, object>, 
                       Action<Dictionary<string, object>>, 
                       Dictionary<string, object>>(
                                                   (windowName, 
                                                    parentWindowName, 
                                                    inputFields, 
                                                    closeCallBack, 
                                                    windowProperties) =>
                       closeCallBack(windowProperties /* or whatever dictionary should go here*/)
                                                    );

这样,传递给ShowDialog的参数将发送给Action方法的Callback。调用ShowDialog时,将调用Action<Dictionary<string, object>> closeCallBack作为{{1}}。