我经常想测试一下使用正确的参数调用了一个方法,但是我发现没有使用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();总结
以上是内存溢出为你收集整理的c# – 将参数传递给FakeItEasy-mock而不使用魔术字符串?全部内容,希望文章能够帮你解决c# – 将参数传递给FakeItEasy-mock而不使用魔术字符串?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)