Unit of Work の実装には、とりわけ次の方法があります。
T Single<T>(Expression<Func<T, bool>> expression) where T : class, new();
たとえば、次のように呼びます。
var person = _uow.Single<Person>(p => p.FirstName == "Sergi");
Single
メソッドが引数で呼び出されたことを確認するにはどうすればよいFirstName == "Sergi"
ですか?
私は次のことを試しましたが、役に立ちませんでした:
// direct approach
session.Verify(x => x.Single<Person>(p => p.FirstName == "Sergi"));
// comparing expressions
Expression<Func<Person, bool>> expression = p => p.FirstName == "Sergi");
session.Verify(x => x
.Single(It.Is<Expression<Func<Person, bool>>>(e => e == expression));
それらはすべて次のエラーになります。
少なくとも 1 回はモックでの呼び出しが期待されていましたが、実行されませんでした
それをどのように行うことができるかについてのアイデアはありますか? NuGetの最新のMoqバージョン 4.0.10827.0 を使用しています。
更新: 具体例
私が見ているのは、ラムダ内で文字列リテラルを使用するたびに機能することVerify
です。変数を比較するとすぐに失敗します。適例:
// the verify
someService.GetFromType(QuestionnaireType.Objective)
session.Verify(x => x.Single<Questionnaire>(q =>
q.Type == QuestionnaireType.Objective));
// QuestionnaireType.Objective is just a constant:
const string Objective = "objective";
// the method where it's called (FAILS):
public Questionnaire GetFromType(string type)
{
// this will fail the Verify
var questionnaire = _session
.Single<Questionnaire>(q => q.Type == type);
}
// the method where it's called (PASSES):
public Questionnaire GetFromType(string type)
{
// this will pass the Verify
var questionnaire = _session
.Single<Questionnaire>(q => q.Type == QuestionnaireType.Objective);
}
Verify
ラムダ式でメソッド パラメータを使用するとすぐに失敗するのはなぜですか?
このテストを書く適切な方法は何でしょうか?