在不使用魔术字符串的情况下将参数传递给FakeItEasy-mock?

在过去的几年里,我一直在使用Moq来满足我的嘲笑需求,但在看了FakeItEasy之后我想尝试一下。

我经常想测试一个方法是否使用正确的参数调用,但我发现没有令人满意的方法来使用FakeItEasy。

我有以下代码来测试:

public class WizardStateEngine : IWizardStateEngine { private readonly IWorkflowInvoker _workflowInvoker; private List _history; public WizardStateEngine(IWorkflowInvoker workflowInvoker) { _workflowInvoker = workflowInvoker; } public void Initialize(List history) { _history = history; } public WizardStateContext Execute(Command command, WizardStateContext stateContext, CustomBookmark step) { Activity workflow = new MyActivity(); var input = new Dictionary(); input["Action"] = command; input["Context"] = stateContext; input["BookmarkHistory"] = _history; var output = _workflowInvoker.Invoke(workflow, input); _history = output["BookmarkHistory"] as List; return output["Context"] as WizardStateContext; } public List GetBookmarkHistory() { return _history; } } 

我想编写一些测试来validation_workflowInvoker.Invoke()的输入。 我的TestInitialize方法设置所需的资源,并将输入字典保存为_workflowInvoker.Invoke()作为本地字段_wfInput。

  [TestInitialize] public void TestInitialize() { _wizardStateContext = new WizardStateContext(); _workflowInvoker = A.Fake(); _wizardStateEngine = new WizardStateEngine(_workflowInvoker); _outputContext = new WizardStateContext(); _outputHistory = new List(); _wfOutput = new Dictionary {{"Context", _outputContext}, {"BookmarkHistory", _outputHistory}}; _history = new List(); A.CallTo(() => _workflowInvoker.Invoke(A.Ignored, A<Dictionary>.Ignored)) .Invokes(x => _wfInput = x.Arguments.Get<Dictionary>("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) _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._, A<Dictionary>.That.Matches( x => (WizardStateContext) x["Context"] == _wizardStateContext))) .MustHaveHappened(); } 

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

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

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

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

我同意达林所说的一切,做你正在做的事似乎是一种坏习惯。 你说这个琐碎的例子看起来很“愚蠢”,你能提供一个看起来很聪明的例子吗?

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

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