【问题标题】:Getting arguments passed to a FakeItEasy-mock without using magic strings?在不使用魔术字符串的情况下将参数传递给 FakeItEasy-mock?
【发布时间】:2011-10-31 06:48:53
【问题描述】:

过去几年我一直在使用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 方法设置了所需的资源并将输入字典作为本地字段 _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 中的魔术字符串“输入”来获取传递的参数(或幻数)。我可以像这样在没有本地字段的情况下编写测试:

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

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

有没有什么方法可以将输入保存为我的测试类中没有幻数或字符串的字段?

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

【问题讨论】:

  • 关于 Darin Dimitrov 和 Patrik Hägne 的回答:如果您的单元测试结构对每个正在测试的方法使用不同的测试夹具(如 Phil Haack 在 this 帖子中所示),那么放置设置逻辑非常有意义进入设置方法。为了通过单一方法测试许多不同的事实,您将使用基本相同的设置,对每个要测试的事实进行微小的更改。另请参阅 Brian Rigsby 方法 here

标签: c# unit-testing moq fakeiteasy


【解决方案1】:
A.CallTo(() => service.DoSomething(A<int>.That.Matches(x => x == 100)))
 .MustHaveHappened();

【讨论】:

  • 感谢您的回答,但这并不是我想要的。我想将输入保存为我的测试类中的一个字段。这样我可以在 SetUp 中设置一次,然后在多个测试中使用局部变量。对于复杂的对象,表达式可能很难阅读。生病更新我的问题..
  • @Olsenius,你为什么需要这样做?您可以在 lambda 中执行必要的断言。您不再需要致电inputNumber.ShouldEqual(100);
  • 我更新了我的问题。我想将输入保存为多个测试的字段,并且只设置一次。
  • @Olsenius,您正在尝试做的是错误的做法恕我直言。单元测试应该彼此独立,并且您不应该让一个测试初始化​​应该由另一个测试使用的数据。另一方面,您可以编写一个函数,该函数将在每个测试的A&lt;int&gt;.That.Matches 约束中调用以验证数据。
  • 实际上在这种情况下没有理由使用参数约束“A.That.Matches(x => x == 100)”。只需使用“A.CallTo(() => service.DoSomething(100)) .MustHaveHappened();”
【解决方案2】:

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

无论如何,以下测试的行为与 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);
}

【讨论】:

  • 我用我要测试的实际课程更新了我的问题。我也不想使用幻数。
  • 最小起订量的实现完全一样,就像魔术一样。它要求第一个参数是 int,就像 FIE 一样。
猜你喜欢
  • 1970-01-01
  • 2013-01-18
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 2011-12-05
  • 2014-04-13
  • 2011-04-30
相关资源
最近更新 更多