私は過去数年間、モッキングのニーズに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 のマジック文字列 "input" は好きではありません。次のように、ローカル フィールドなしでテストを記述できます。
[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();
}
しかし、ローカルフィールドを使用したテストの方が読みやすいと思います。
マジック ナンバーや文字列を使用せずに、テスト クラスのフィールドとして入力を保存するように設定する方法はありますか?
質問の更新された例が、ローカル フィールドを使用したい理由を示していることを願っています。読みやすい方法が見つかれば、 local フィールドを使わずにテストを書きたいと思っています。