获取传递给FakeItEasy-mock的参数,而不使用魔法字符串

本文关键字:字符串 FakeItEasy-mock 参数 获取 | 更新日期: 2023-09-27 18:06:07

过去几年我一直在使用Moq来满足我的嘲笑需求,但在看了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方法设置所需的资源,并将输入字典保存到_workflowInvoker.Invoke()中,作为一个本地字段_wfInput。

    [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中用于获取传递参数(或幻数)的神奇字符串"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();
    }

但是我发现使用local字段的测试更具可读性。

有没有办法设置保存输入作为一个字段在我的测试类没有魔术数字或字符串?

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

获取传递给FakeItEasy-mock的参数,而不使用魔法字符串

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

我同意Darin所说的一切,做你正在做的事情似乎是一种不好的做法。你说在这个微不足道的例子中它看起来很"愚蠢",你能提供一个看起来很聪明的例子吗?

无论如何,下面的测试将具有与moq测试完全相同的行为:

[Test]
public void Should_do_something_with_correct_input()
{
    int inputNumber = 0;
    var service = A.Fake<IService>();
    A.CallTo(() => service.DoSomething(A<int>._))
        .Invokes((int x) => inputNumber = x);
    var system = new System(service);
    system.InvokeService();
    inputNumber.ShouldEqual(100);
}