c# - 在不使用魔术字符串的情况下将参数传递给 FakeItEasy-mock?

标签 c# unit-testing moq fakeiteasy

我一直在使用Moq因为我过去几年的 mock 需要,但在看了FakeItEasy之后我想试一试。

我经常想测试是否使用正确的参数调用了一个方法,但我发现使用 FakeItEasy 没有令人满意的方法。

我有以下代码要测试:

    public class WizardStateEngine : IWizardStateEngine
{
    private readonly IWorkflowInvoker _workflowInvoker;
    private List<CustomBookmark> _history;

    public WizardStateEngine(IWorkflowInvoker workflowInvoker)
    {
        _workflowInvoker = workflowInvoker;
    }

    public void Initialize(List<CustomBookmark> history)
    {
        _history = history;
    }

    public WizardStateContext Execute(Command command, WizardStateContext stateContext, CustomBookmark step)
    {
        Activity workflow = new MyActivity();
        var input = new Dictionary<string, object>();
        input["Action"] = command;
        input["Context"] = stateContext;
        input["BookmarkHistory"] = _history;

        var output = _workflowInvoker.Invoke(workflow, input);

        _history = output["BookmarkHistory"] as List<CustomBookmark>;

        return output["Context"] as WizardStateContext;
    }

    public List<CustomBookmark> GetBookmarkHistory()
    {
        return _history;
    }
}

我想编写一些测试来验证对 _workflowInvoker.Invoke() 的输入。 我的 TestInitialize 方法设置了所需的资源并将输入字典作为本地字段 _wfInput 保存到 _workflowInvoker.Invoke()。

    [TestInitialize]
    public void TestInitialize()
    {
        _wizardStateContext = new WizardStateContext();
        _workflowInvoker = A.Fake<IWorkflowInvoker>();
        _wizardStateEngine = new WizardStateEngine(_workflowInvoker);

        _outputContext = new WizardStateContext();
        _outputHistory = new List<CustomBookmark>();
        _wfOutput = new Dictionary<string, object>
                        {{"Context", _outputContext}, {"BookmarkHistory", _outputHistory}};

        _history = new List<CustomBookmark>();

        A.CallTo(() =>
                 _workflowInvoker.Invoke(A<Activity>.Ignored, A<Dictionary<string, object>>.Ignored))
            .Invokes(x => _wfInput = x.Arguments.Get<Dictionary<string, object>>("input"))
            .Returns(_wfOutput);

        _wizardStateEngine.Initialize(_history);
    }

设置后我有多个这样的测试:

    [TestMethod]
    public void Should_invoke_with_correct_command()
    {
        _wizardStateEngine.Execute(Command.Start, null, null);

        ((Command) _wfInput["Action"]).ShouldEqual(Command.Start);
    }

    [TestMethod]
    public void Should_invoke_with_correct_context()
    {
        _wizardStateEngine.Execute(Command.Start, _wizardStateContext, null);

        ((WizardStateContext) _wfInput["Context"]).ShouldEqual(_wizardStateContext);
    }

    [TestMethod]
    public void Should_invoke_with_correct_history()
    {
        _wizardStateEngine.Execute(Command.Start, _wizardStateContext, null);

        ((List<CustomBookmark>) _wfInput["BookmarkHistory"]).ShouldEqual(_history);
    }

我不喜欢 TestInitialize 中用于获取传递参数(或魔数(Magic Number))的魔法字符串“input”。我可以像这样在没有本地字段的情况下编写测试:

    [TestMethod]
    public void Should_invoke_with_correct_context()
    {
        _wizardStateEngine.Execute(Command.Start, _wizardStateContext, null);

        A.CallTo(() =>
                 _workflowInvoker.Invoke(A<Activity>._,
                                         A<Dictionary<string, object>>.That.Matches(
                                             x => (WizardStateContext) x["Context"] == _wizardStateContext)))
            .MustHaveHappened();
    }

但我发现本地字段的测试更具可读性。

有没有什么方法可以在我的测试类中设置将输入保存为一个字段而无需魔数(Magic Number)或字符串?

我希望问题中更新的示例能够说明我为什么要使用本地字段。如果我能找到一种很好的可读方式,我非常愿意在没有本地字段的情况下编写我的测试。

最佳答案

A.CallTo(() => service.DoSomething(A<int>.That.Matches(x => x == 100)))
 .MustHaveHappened();

关于c# - 在不使用魔术字符串的情况下将参数传递给 FakeItEasy-mock?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7133201/

相关文章:

unit-testing - 用于测试目的的假 http 服务器

java - 截断所有表,清除一级和二级 hibernate 缓存的简单方法?

c# - 使第三方库实现接口(interface)的策略?

c# - 使用 "for"循环帮助遍历充满对象的数组列表

c# - 序列化“System.Data.Entity.DynamicProxies”类型的对象时检测到循环引用

c# - 如何解决 "Runtime error Exception has been thrown by the target of an invocation"我是否缺少 DLL 文件?

c# - 单元测试代码的最佳位置

C# 单元测试复杂处理程序网络

c# - 使用 Moq 对继承层次结构进行单元测试

c# - LINQ to SQL 中更好的 UPDATE 方法