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();

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么