获取传递给FakeItEasy,模拟参数不使用魔法字符串? [英] Getting arguments passed to a FakeItEasy-mock without using magic strings?

查看:132
本文介绍了获取传递给FakeItEasy,模拟参数不使用魔法字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用起订量我嘲讽需要近几年,但是看着的FakeItEasy 我想给它一个尝试。



我常常想测试。一种方法被称为使用正确的参数,但是我发现没有令人满意的方式与FakeItEasy做到这一点。



我有下面的代码进行测试:

 公共类WizardStateEngine:IWizardStateEngine 
{
私人只读IWorkflowInvoker _workflowInvoker;
私有列表< CustomBookmark> _历史;

公共WizardStateEngine(IWorkflowInvoker workflowInvoker)
{
_workflowInvoker = workflowInvoker;
}

公共无效初始化(列表< CustomBookmark>历史)
{
_history =历史;
}

公共WizardStateContext执行(Command命令,WizardStateContext状态上下文,CustomBookmark步)
{
活动流程=新MyActivity();
VAR输入=新词典<字符串对象>();
输入[行动] =命令;
输入[上下文] =状态上下文;
输入[BookmarkHistory] = _history;

无功输出= _workflowInvoker.Invoke(工作流,输入);

_history =输出[BookmarkHistory]作为名单< CustomBookmark取代;

返回输出[语境]作为WizardStateContext;
}

公开名单< CustomBookmark> GetBookmarkHistory()
{
返回_history;
}
}



我想写一些测试,验证输入_workflowInvoker.Invoke()。
我TestInitialize方法设置所需要的资源,并保存在本地场_wfInput输入字典_workflowInvoker.Invoke()。

  [TestInitialize] 
公共无效TestInitialize()
{
_wizardStateContext =新WizardStateContext();
_workflowInvoker = A.Fake< IWorkflowInvoker>();
_wizardStateEngine =新WizardStateEngine(_workflowInvoker);

_outputContext =新WizardStateContext();
_outputHistory =新的List< CustomBookmark>();
_wfOutput =新词典<字符串对象>
{{背景,_outputContext},{BookmarkHistory,_outputHistory}};

_history =新的List< CustomBookmark>();

A.CallTo(()=>
_workflowInvoker.Invoke(A<活动> .Ignored,读取<字典<字符串对象>> .Ignored))
.Invokes(X => _wfInput = x.Arguments.Get<&字典LT;字符串对象>>(输入))
.Returns(_wfOutput);

_wizardStateEngine.Initialize(_history);
}



设置完成后,我有多次测试这样的:

  [TestMethod的] 
公共无效Should_invoke_with_correct_command()
{
_wizardStateEngine.Execute(Command.Start,NULL,NULL );

((命令)_wfInput [行动])ShouldEqual(Command.Start)。
}

[TestMethod的]
公共无效Should_invoke_with_correct_context()
{
_wizardStateEngine.Execute(Command.Start,_wizardStateContext,NULL);

((WizardStateContext)_wfInput [语境])ShouldEqual(_wizardStateContext)。
}

[TestMethod的]
公共无效Should_invoke_with_correct_history()
{
_wizardStateEngine.Execute(Command.Start,_wizardStateContext,NULL);

((名单< CustomBookmark>)_wfInput [BookmarkHistory])。ShouldEqual(_history);
}



我不喜欢在TestInitialize魔术字符串输入用于获取通过参数(或幻数)。我可以写测试,而像这样的局部领域:

  [TestMethod的] 
公共无效Should_invoke_with_correct_context()
{
_wizardStateEngine.Execute(Command.Start,_wizardStateContext,NULL);

A.CallTo(()=>
_workflowInvoker.Invoke(A<活动> ._,
A<字典<字符串对象>> .That.Matches (
X =>(WizardStateContext)×[背景] == _wizardStateContext)))
.MustHaveHappened();
}



但我发现当地实地测试更具可读性。



是否有什么办法可以将输入作为一个字段的设置保存我在我的测试类无幻数或弦?



我希望在问题的更新例子表明为什么我想用当地的领域。我更愿意写我的测试,而当地的领域,如果我能找到一个不错的易读的方式做多。


解决方案

  A.CallTo(()=> service.DoSomething(A< INT> ; .That.Matches(X => X == 100)))
.MustHaveHappened();


I have been using Moq for my mocking needs the last years, but after looking at FakeItEasy i wanted to give it a try.

I often want to test that a method have been called with the correct parameters, but i found no satisfactory way to do this with FakeItEasy.

I have the following code to test:

    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;
    }
}

I want to write some tests that verifies the input to _workflowInvoker.Invoke(). My TestInitialize method sets up the needed resources and save the input dictionary to _workflowInvoker.Invoke() as a local field _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);
    }

After the setup i have multiple tests like this:

    [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);
    }

I do not like the magic string "input" in the TestInitialize for getting the passed argument (or magic number). I can write the tests without the local field like this:

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

But i find the tests with the local field more readable.

Are there any way to setup saving of the input as a field i my test class without magic numbers or strings?

I hope the updated example in the question shows why i would like to use the local field. I am more than willing to write my tests without the local field if i can find a nice readable way to do it.

解决方案

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

这篇关于获取传递给FakeItEasy,模拟参数不使用魔法字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆