c# – 将参数传递给FakeItEasy-mock而不使用魔术字符串?

前端之家收集整理的这篇文章主要介绍了c# – 将参数传递给FakeItEasy-mock而不使用魔术字符串?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在使用 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,null);

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

我不喜欢TestInitialize中的魔术字符串“input”来获取传递的参数(或魔术数字).我可以在没有本地字段的情况下编写测试:

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

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

但是我发现与本地字段的测试更易读.

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

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

解决方法

A.CallTo(() => service.DoSomething(A<int>.That.Matches(x => x == 100)))
 .MustHaveHappened();
原文链接:https://www.f2er.com/csharp/94828.html

猜你在找的C#相关文章