简体   繁体   English

Microsoft.Azure.Documents.Client CreateDocumentQuery Moq

[英]Microsoft.Azure.Documents.Client CreateDocumentQuery Moq

I am trying to create a moq for Microsoft.Azure.Documents.Client CreateDocumentQuery我正在尝试为 Microsoft.Azure.Documents.Client CreateDocumentQuery 创建最小起订量

I created an interface with class implementation where I instantiate an object of DocumentClient and make corresponding calls.我创建了一个带有类实现的接口,在其中实例化 DocumentClient 的对象并进行相应的调用。 This is to help moq easy for this.这是为了帮助 moq 轻松实现这一点。

Here are the code:下面是代码:

public interface IASDocumentClient
{
    IOrderedQueryable<T> CreateDocumentQuery<T>(Uri documentCollectionUri, FeedOptions feedOptions = null);
}

public class ASDocumentClient : IASDocumentClient
{
    DocumentClient client = null;
    public ASDocumentClient(DocumentClient documentClient)
    {
        this.client = documentClient;
    }


    public IOrderedQueryable<Document> CreateDocumentQuery(Uri documentCollectionOrDatabaseUri, FeedOptions feedOptions = null)
    {
        return client.CreateDocumentQuery(documentCollectionOrDatabaseUri, feedOptions);
    }        

    public IQueryable<T> CreateDocumentQuery<T>(Uri documentCollectionOrDatabaseUri, SqlQuerySpec querySpec, FeedOptions feedOptions = null)
    {
        return client.CreateDocumentQuery<T>(documentCollectionOrDatabaseUri, querySpec, feedOptions);
    }
}




   public class DocumentDBRepositoryUnitTest : IDisposable
    {
        IDocumentDBRepository<TestDocumentDBEntity> documentDBRepository;
        List<TestDocumentDBEntity> items = new List<TestDocumentDBEntity>();

        //Pre-test
        public DocumentDBRepositoryUnitTest()
        {
            Mock<IASDocumentClient> documentClient = new Mock<IASDocumentClient>();

            documentClient.Setup(x => x.CreateDocumentQuery<Document>(It.IsAny<Uri>(), It.IsAny<FeedOptions>())).Returns(queryDocuments);
        }


        //Not working now
        [Fact]
        public void GetItemsAsyncTest()
        {
            //Arrange 

            //Act
            var retTask = documentDBRepository.GetItemsAsync(x => true);

            //Assert
            Assert.NotNull(retTask);
            Assert.NotNull(retTask.Result);
        }

        //Post-test
        public void Dispose()
        {
            items = new List<TestDocumentDBEntity>();
        }
    }


public class DocumentDBRepository<T> : IDocumentDBRepository<T> where T : BaseDocumentDBEntity, new()
{
    private string cosmosDbUri;
    private string cosmosDbAuthKey;
    private string databaseId;
    private string collectionId;
    private IASDocumentClient client=null;

    public DocumentDBRepository(IASDocumentClient client, string databaseId, string collectionId)
    {
        this.client = client;
        this.databaseId = databaseId;
        this.collectionId = collectionId;
        if (!string.IsNullOrEmpty(this.databaseId) && !string.IsNullOrEmpty(this.collectionId))
            Initialize();
    }

    public async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate)
    {
      IDocumentQuery<T> query = client.CreateDocumentQuery<T>(
          UriFactory.CreateDocumentCollectionUri(this.databaseId, this.collectionId), new FeedOptions { MaxItemCount = -1 })
        .Where(predicate)
        .AsDocumentQuery();

      List<T> results = new List<T>();
      while (query.HasMoreResults)
      {
        results.AddRange(await query.ExecuteNextAsync<T>());
      }

      return results;
    }
}

When I run the test, its not even reaching out to the mock Setup for CreateDocumentQuery:当我运行测试时,它甚至没有接触到 CreateDocumentQuery 的模拟设置:

documentClient.Setup(x => x.CreateDocumentQuery(It.IsAny(), It.IsAny())).Returns(queryDocuments); documentClient.Setup(x => x.CreateDocumentQuery(It.IsAny(), It.IsAny())).Returns(queryDocuments);

Any idea?任何的想法?

The method you are trying to mock is non-virtual.您尝试模拟的方法是非虚拟的。 You can only mock abstract methods on a class or mock an interface of a class.您只能模拟类上的抽象方法或模拟类的接口。

https://github.com/Moq/moq4/wiki/Quickstart - you can see here that IFoo is an interface and Bar and Baz are concrete classes with abstract methods. https://github.com/Moq/moq4/wiki/Quickstart - 你可以在这里看到IFoo是一个接口,而BarBaz是具有抽象方法的具体类。

I would suggest you create an abstraction on top of this code.我建议您在此代码之上创建一个抽象。 The repository pattern looks to be suitable in this case as you seem to be persisting data.在这种情况下,存储库模式看起来很合适,因为您似乎要持久化数据。 This will allow you to mock the repository you create wherever it will be used eg in some kind of service layer.这将允许您模拟您创建的存储库,无论它将在何处使用,例如在某种服务层中。 As for testing the actual implementation (the bit where you use the Azure SDK) I would suggest writing an integration test that includes this dependency actually saving the data, either to Storage Emulator or a storage account in Azure.至于测试实际实现(您使用 Azure SDK 的部分),我建议编写一个集成测试,其中包含实际将数据保存到存储模拟器或 Azure 中的存储帐户的此依赖项。

ASDocumentClient does nothing its a 1: 1 wrapper around client so test has negative value ( 0 value + the cost of having the test). ASDocumentClient 不做任何围绕客户端的 1:1 包装,因此测试具有负值(0 值 + 进行测试的成本)。 You should also not unit test DocumentDB unless you find a specific issue that's Microsofts job ( that can be picked up with integration tests)您也不应该对 DocumentDB 进行单元测试,除非您发现 Microsoft 工作的特定问题(可以通过集成测试找到)

IDocumentDBRepository should be tested and can be via your wrapper interface. IDocumentDBRepository 应该被测试并且可以通过你的包装器接口。

Not having ASDocumentClient / IASDocumentClient wrappers to test Repository is a different but useful question.没有 ASDocumentClient / IASDocumentClient 包装器来测试 Repository 是一个不同但有用的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法加载文件或程序集&#39;Microsoft.Azure.Documents.Client - Azure-Table-Api - Could not load file or assembly 'Microsoft.Azure.Documents.Client - Azure-Table-Api Microsoft.Azure.Documents.Linq不支持.Select - .Select is not supported in Microsoft.Azure.Documents.Linq 选择语句使用Microsoft.Azure.Documents - Select statement use Microsoft.Azure.Documents BulkExecotor抛出了Microsoft.Azure.Documents.InvalidPartitionException - BulkExecotor threw Microsoft.Azure.Documents.InvalidPartitionException 例外:从DocumentDB查询时出现Microsoft.Azure.Documents.RequestRateTooLargeException - Exception: Microsoft.Azure.Documents.RequestRateTooLargeException while querying from DocumentDB Microsoft.Azure.Documents.DocumentClientException:服务器无法解析 Url - Microsoft.Azure.Documents.DocumentClientException: Server could not parse the Url 用于Azure存储托管文档的Microsoft Office Web查看器 - Microsoft Office web viewer for Azure Storage hosted documents 我的Azure DocumentDB文档类是否应继承自Microsoft.Azure.Documents.Document? - Should my Azure DocumentDB document classes inherit from Microsoft.Azure.Documents.Document? “Microsoft.Azure.Devices.Client.Message”和“Microsoft.Azure.Devices.Message”之间的不明确引用 - Ambiguous reference between 'Microsoft.Azure.Devices.Client.Message' and 'Microsoft.Azure.Devices.Message 如何允许客户端IP访问Microsoft Azure数据库? - How to allow client IP access to Microsoft Azure database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM