【发布时间】:2012-08-07 20:53:30
【问题描述】:
如有必要,您将如何使用 Moq 和 Moles 测试以下代码?
我想检查在引发“SomeEvent”时是否调用了一次“TestCommand.RaiseCanExecuteChanged()”命令。
困难在于 RaiseCanExecuteChanged 是 Microsoft.Practices.Prism.Commands.DelegateCommandBase 上的公共非虚拟方法,而 Moles 引入了其他复杂性,其中之一是它似乎无法仅模拟一个没有返回值。
using Microsoft.Practices.Prism.Commands;
public class SomeEventClass
{
public event Action SomeEvent;
}
public class TestClass
{
public TestClass(SomeEventClass someEventClass)
{
TestCommand = new DelegateCommand(() => { /* do stuff */}, CanExecute);
someEventClass.SomeEvent += () => TestCommand.RaiseCanExecuteChanged();
}
private bool CanExecute()
{
//Some logic
return true;
}
public DelegateCommand TestCommand { get; private set; }
}
【问题讨论】: