Мокинг в модульном тесте (C#) не попадает в мою настройку - PullRequest
0 голосов
/ 14 июля 2020

У меня есть следующий код:

            // Arrange
            // to be return by the called mock
            Mock<ItemResponse<SchemaStore>> mockResponse = new Mock<ItemResponse<SchemaStore>>();
            mockResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.NotFound);

            // data to be passed to the method under test
            var id = "mockSchemaId_NotFound";
            var partitionKey = new PartitionKey("mockPK");
            var mockContainer = new Mock<Container>();

            // set the mock expected behavior
            mockContainer.Setup(x => x.ReadItemAsync<SchemaStore>(
               It.IsAny<string>(),
               It.IsAny<PartitionKey>(),
               It.IsAny<ItemRequestOptions>(),
               It.IsAny<CancellationToken>()))
                .ReturnsAsync(mockResponse.Object);

            // Act
            var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appSettings.junk.json", optional: true, reloadOnChange: true);
            var configuration = builder.Build();
            SchemaManager manager = new SchemaManager(new SchemaDataSource(configuration,
                new FailOverCosmosClient(configuration)));
            try
            {
                var x = await manager.GetSchemaAsync(id);
                throw new Exception("test failed");
            }
            catch (Exception ex)
            {
                mockContainer.Verify();
                return;
            }
            // Assert
            //mockContainer.Verify();
        }

В коде продукта ScheamManager.GetSchemaAsyn c будет вызывать SchemaDataSource.GetSchemaAsyn c, который вызывает метод GetSchemaAsyn c FailOverCosmosClient. Там он вызовет container.ReadItemAsyn c после того, как получит контейнер.

                    try
                    {
                        Container container = client.GetDatabase(this.cosmosDbDatabaseName).GetContainer(collectionName);
                        ItemResponse<T> response = await container.ReadItemAsync<T>(id: documentId, partitionKey: new PartitionKey(partitionKey)).ConfigureAwait(false);
                        resultStatus = Success;

                        return response.Resource;
                    }
                    catch (CosmosException ex)
                    {

Но он все равно попадает в настоящий контейнер - несмотря на мои настройки. Может ли кто-нибудь предложить помощь?

Большое спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...