【问题标题】:Test non-virtual public method with Moq and Moles?用 Moq 和 Moles 测试非虚拟公共方法?
【发布时间】: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; }
}

【问题讨论】:

    标签: c# moq moles


    【解决方案1】:

    Moles 应该能够填充没有返回类型的方法。有些东西阻止了 Moles 绕道这个方法。

    我建议使用存根和依赖注入,而不是构建模拟(这可能需要一段时间)或使用 Moles 填充调用。

    【讨论】:

    • 你是对的,只是 Moles 新手的错误。现在如何让鼹鼠和起订量一起玩,但这是另一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 2015-12-17
    • 2012-07-25
    相关资源
    最近更新 更多