c# – 将参数传递给FakeItEasy-mock而不使用魔术字符串?
发布时间:2020-12-15 06:44:14 所属栏目:百科 来源:网络整理
导读:我一直在使用 Moq作为我最近几年的嘲笑需求,但是在看了 FakeItEasy后,我想试试看. 我经常想测试一下使用正确的参数调用了一个方法,但是我发现没有使用FakeItEasy的方法. 我有以下代码来测试: public class WizardStateEngine : IWizardStateEngine{ private
我一直在使用
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] 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(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |