因此,我正在尝试从注入的对话框API中测试方法(使用CaliburnMicro MVVM框架)。
方式
public bool? ShowDialog(Screen dialog)
{
dynamic settings = new ExpandoObject();
settings.WindowStartupLocation = WindowStartupLocation.CenterOwner;
settings.ResizeMode = ResizeMode.NoResize;
return _dialogAPI.ShowDialog(dialog, null, settings);
}
这就是我试图测试它的方式:
测试
[Fact]
public void DialogServiceCallAPIToShowDialog()
{
var dialogAPI = new Mock<IWindowManager>();
dialogAPI.Setup(x => x.ShowDialog(It.IsAny<Screen>(), null, It.IsAny<Dictionary<string, object>>())).Returns(() => true);
var instance = new DialogService(dialogAPI.Object);
instance.ShowDialog(It.IsAny<Screen>());
dialogAPI.Verify(x => x.ShowDialog(It.IsAny<Screen>(), null, It.IsAny<Dictionary<string, object>>()), Times.Once, "Fail...");
}
问题
XUnit没有告诉我这个:
消息:Moq.MockException:&#34;失败...&#34;模拟上的预期调用 一次,但是0次:
x => x.ShowDialog(It.IsAny<Screen>(), null, It.IsAny<Dictionary<String, Object>>())
已配置的设置:
x => x.ShowDialog(It.IsAny<Screen>(), null, It.IsAny<Dictionary<String, Object>>())
执行调用:IWindowManager.ShowDialog(null,null, [[WindowStartupLocation,CenterOwner],[ResizeMode,NoResize]])
我认为这与我传递给Is.Any
方法的数据类型有关,但我不确定。有什么想法吗?
答案 0 :(得分:3)
我认为你需要替换:
It.IsAny<IDictionary<string, object>>()
与
Verify
在ExpandoObject
来电中。由于您ShowDialog
IDictionary
传递了Dictionary
,但public class Screen {
}
public interface IWindowManager {
bool? ShowDialog(object rootModel, object context = null, IDictionary<string, object> settings = null);
}
public class DialogService {
private IWindowManager _dialogAPI;
public DialogService(IWindowManager dialogAPI) {
_dialogAPI = dialogAPI;
}
public virtual bool? ShowDialog(Screen dialog)
{
dynamic settings = new ExpandoObject();
settings.WindowStartupLocation = WindowStartupLocation.CenterOwner;
settings.ResizeMode = ResizeMode.NoResize;
return _dialogAPI.ShowDialog(dialog, null, settings);
}
}
不是Dictionary
。
这是我测试过的代码:
IDictionary
您的代码失败了,而将var dialogAPI = new Mock<IWindowManager>();
dialogAPI.Setup(x => x.ShowDialog(It.IsAny<Screen>(), null, It.IsAny<Dictionary<string, object>>())).Returns(() => true);
var instance = new DialogService(dialogAPI.Object);
instance.ShowDialog(It.IsAny<Screen>());
dialogAPI.Verify(x => x.ShowDialog(It.IsAny<Screen>(), null, It.IsAny<IDictionary<string, object>>()), Times.Once, "Fail...");
替换为{{1}}则传递正常:
{{1}}