リポジトリレイヤーをモックしようとしていますが、メソッドがありGetSelection(Expression<Func<T, Boolean>> where)
ます。これを実現するために、MoqでNinjects MickingKernelを使用しています。
私が次のことをすると、問題ありません。
// Create instance of repository
var kernel = new MoqMockingKernel();
var templateRepoMock = kernel.GetMock<ITemplateRepo>();
// Setup mock
templateRepoMock.Setup(x=>x.GetSelection(y=>y.FieldName)
.Returns(new List<Template>() {new Template { ... }));
// Lets pretend that FieldName is a bool!
// Call Service layer
var result = templateService.MyMethod();
// -> Service Layer method
public List<Template> MyMethod()
{
return _templateRepo.GetSelection(x=>x.FieldName);
}
しかし、式に追加のパラメーターを追加しようとすると、次のようになりますArgumentNullExeption
。
// Setup mock
templateRepoMock.Setup(x=>x.GetSelection(y=>y.SomeOtherField.Id == 1
&& y.FieldName)
.Returns(new List<Template>() {new Template { ... }));
サービスを次のように更新すると:
public List<Template> MyMethod(SomeObject myObject)
{
return _templateRepo.GetSelection(x=>x.SomeObject.Id == myObject.Id
&& x.FieldName).ToList();
}
myObject.Id を 1 に更新すれば問題ないようです。
なぜこれが起こっているのでしょうか?