0

リポジトリレイヤーをモックしようとしていますが、メソッドがあり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 に更新すれば問題ないようです。

なぜこれが起こっているのでしょうか?

4

1 に答える 1

1

Github プロジェクトを使用している場合でも、例外はまだ表示されません。代わりに、VerifyAll次のメッセージの行でテストが失敗します。

Moq.MockVerificationException : 次の設定が一致しませんでした:

IProductRepository mock => mock.GetProducts(x => x.Name == "Test" && x.IsActive)

ただし、Moq はExpression. この回答を参照してください。

于 2014-08-07T15:28:51.790 に答える