2017-02-26 48 views
0

我試圖對彈性客戶端調用Moq,但它不起作用。模擬彈性客戶端調用

我有這個調用內部的通用repo.Update(T doc)

var response = await _client.UpdateAsync(DocumentPath<T>.Id(doc), 
    d => d 
     .Upsert(doc) 
     .Script(.....) 
); 

在我的測試我想驗證是否UpdateAsync與這些特定的輸入調用。

我試過,沒有工作的情況如下:我覺得你有與第一驗證條件問題

_mock = new Mock<IElasticClient>(); 

Func<UpdateDescriptor<Document,Document>, 
    IUpdateRequest<Document,Document>> sel = d => d.Upsert(doc).Script(....); 

await repo.Update(document) 

_mock.Verify(c => c.UpdateAsync<Document>(
    It.Is<DocumentPath<Document>(
     docPath => docPath == DocumentPath<Document>.Id(document), 
    It.Is<Func<...>(s => s == sel), 
    it.IsAny<CancellationToken>() 
); 

回答

1

docPath => docPath == DocumentPath<Document>.Id(document) 

正如你可以看到NEST source codeDocumentPath類不重載==運算符 - 它檢查引用相等。 DocumentPath.Id(document)創建新對象,因此條件始終爲false。

希望有所幫助。