如何编写与 Cosmos 数据库相关的存储库单元测试(SQLApi + CosmosClient)

SAR*_*ASH 6 c# unit-testing mstest asp.net-core azure-cosmosdb

我有一堂课如下:

 public class CosmosRepository: ICosmosRepository
 {
        private ICosmoDBSettings cosmoDbSettings;

        private CosmosClient cosmosClient;

        private static readonly object syncLock = new object();

        // The database we will create
        private Database database;

        public CosmosRepository(ICosmoDBSettings cosmoDBSettings)
        {
            this.cosmoDbSettings = cosmoDBSettings;

            this.InitializeComponents();
        }

        private void InitializeComponents()
        {
            try
            {
                if (cosmosClient != null)
                {
                    return;
                }

                lock (syncLock)
                {
                    if (cosmosClient != null)
                    {
                        return;
                    }

                    this.cosmosClient = new CosmosClient(
                        cosmoDbSettings.CosmosDbUri
                        , cosmoDbSettings.CosmosDbAuthKey
                        , new CosmosClientOptions
                            {
                                ConnectionMode = ConnectionMode.Direct
                            }
                        );
                    this.database = this.cosmosClient.CreateDatabaseIfNotExistsAsync(cosmoDbSettings.DocumentDbDataBaseName).Result;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的存储库方法为:
不要担心硬编码值。

  public async Task<Employee> GetById()
    {
       var container = this.database.GetContainer("Employees");
       var document = await container.ReadItemAsync<Employee>("44A85B9E-2522-4BDB-891A- 
                      9EA91F6D4CBF", new PartitionKey("PartitionKeyValue"));
       return document.Response;
    }
Run Code Online (Sandbox Code Playgroud)

Note
如何在.NET Core中针对Cosmos数据库编写单元测试(MS单元测试)?
如何模拟 CosmosClient 及其所有方法。有人可以帮我解决这个问题吗?

我的单元测试如下所示:

public class UnitTest1
    {
        private Mock<ICosmoDBSettings> cosmoDbSettings;

        private Mock<CosmosClient> cosmosClient;

        private Mock<Database> database;

        [TestMethod]
        public async Task TestMethod()
        {
            this.CreateSubject();
            var databaseResponse = new Mock<DatabaseResponse>();
            var helper = new CosmosDBHelper(this.cosmoDbSettings.Object);
            this.cosmosClient.Setup(d => d.CreateDatabaseIfNotExistsAsync("TestDatabase", It.IsAny<int>(), It.IsAny<RequestOptions>(), It.IsAny<CancellationToken>())).ReturnsAsync(databaseResponse.Object);
            var mockContainer = new Mock<Container>();
            this.database.Setup(x => x.GetContainer(It.IsAny<string>())).Returns(mockContainer.Object);
            var mockItemResponse = new Mock<ItemResponse<PortalAccount>>();
            mockItemResponse.Setup(x => x.StatusCode).Returns(It.IsAny<HttpStatusCode>);
            var mockPortalAccount = new PortalAccount { PortalAccountGuid = Guid.NewGuid() };
            mockItemResponse.Setup(x => x.Resource).Returns(mockPortalAccount);
            mockContainer.Setup(c => c.ReadItemAsync<PortalAccount>(It.IsAny<string>(),It.IsAny<PartitionKey>(), It.IsAny<ItemRequestOptions>(), It.IsAny<CancellationToken>())).ReturnsAsync(mockItemResponse.Object);

            var pas = helper.GetById().Result;

            Assert.AreEqual(pas.PortalAccountGuid, mockPortalAccount.PortalAccountGuid);


        }
        public void CreateSubject(ICosmoDBSettings cosmoDBSettings = null)
        {
            this.cosmoDbSettings = cosmoDbSettings ?? new Mock<ICosmoDBSettings>();
            this.cosmoDbSettings.Setup(x => x.CosmosDbUri).Returns("https://localhost:8085/");
            this.cosmoDbSettings.Setup(x => x.CosmosDbAuthKey).Returns("C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");
            this.cosmoDbSettings.Setup(x => x.DocumentDbDataBaseName).Returns("TestDatabase");
            this.database = new Mock<Database>();
            this.cosmosClient = new Mock<CosmosClient>();
        }
    }
Run Code Online (Sandbox Code Playgroud)

注意: 异常:响应状态代码并不表示成功:404 子状态:0 原因:(Microsoft.Azure.Documents.DocumentClientException: 消息: {"Errors":["Resource Not Found"]}

我不是在创建文档。我直接获取文档,因为我只返回模拟响应。这是对的吗??