Giter Site home page Giter Site logo

Comments (10)

TsuyoshiUshio avatar TsuyoshiUshio commented on August 16, 2024 1

Hi @j82w ,

From my priority point of view,

  1. CosmosContainer
  2. CosmosDatabase
  3. CosmosClient

For example, I tried to Mock the CreateContainerIfNotExistsAsync, we need this effort. However, it doesn't work because of the error. If we have an interface on it. Things might be much easier.

            var cosmosDatabaseMock = new Mock<CosmosDatabase>();
            var cosmosContainersMock = new Mock<CosmosContainers>();

            var containerResponseMock = new Mock<CosmosContainerResponse>();
            var containerMock = new Mock<CosmosContainer>();
            var container = containerMock.Object;
            containerResponseMock.Setup(p => p.Container).Returns(container);

            cosmosContainersMock.Setup(p => p.CreateContainerIfNotExistsAsync("Todo", "/UserName", null, null, default(CancellationToken)))
                .ReturnsAsync(containerResponseMock.Object);

            cosmosDatabaseMock.Setup(p => p.Containers).Returns(cosmosContainersMock.Object);
            var repository = new GenericRepository<Todo>(cosmosDatabaseMock.Object);
            Assert.Equal(container, repository.container);
            cosmosContainersMock.Verify(p => p.CreateContainerIfNotExistsAsync("Todo", "/UserName", null, null, default(CancellationToken)));

from azure-cosmos-dotnet-v3.

j82w avatar j82w commented on August 16, 2024

Hi Tsuyoshi,

Can you please provide a list of objects that can not be mocked?

from azure-cosmos-dotnet-v3.

TsuyoshiUshio avatar TsuyoshiUshio commented on August 16, 2024

I also try to Mock the method, like this.

                this._containerResponseMock = new Mock<CosmosContainerResponse>();
               

                var expectedCosmosItemResponseMock = new Mock<CosmosItemResponse<Todo>>();
                expectedCosmosItemResponseMock.Setup(p => p.Resource).Returns(ExpectedTodo);
                expectedCosmosItemResponseMock.Setup(p => p.StatusCode).Returns(HttpStatusCode.OK);

                this._containerResponseMock.Setup(p =>
                        p.Container.Items.CreateItemAsync<Todo>(ExpectedPartitionKey, InputTodo, null, default(CancellationToken)))
                    .Returns(Task.FromResult(expectedCosmosItemResponseMock.Object));

it causes

Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: Microsoft.Azure.Cosmos.CosmosContainer.
Could not find a parameterless constructor.

Seems lack of default constructor.

I'm a new to this API, so I might misunderstand the implementation. I'm ok if I can write unit testing. (I usually use TDD method). If my implementation is not proper, please let me know how can I write the unit testing using Moq.

from azure-cosmos-dotnet-v3.

j82w avatar j82w commented on August 16, 2024

We want this to be unit testable. I will create task to add the interface. We are still working on planning so I don't currently have a time frame, but it will be done before the SDK goes GA.

from azure-cosmos-dotnet-v3.

TsuyoshiUshio avatar TsuyoshiUshio commented on August 16, 2024

Awesome! I appreciate you. I'm trying to understand the new API and how to write unit testing for it.
I'll write a blog what I learned and current workaround for unit testing.

from azure-cosmos-dotnet-v3.

j82w avatar j82w commented on August 16, 2024

Thanks for the feedback. If you find any other blockers or have any ideas on how to make it easier to unit test please let us know.

from azure-cosmos-dotnet-v3.

darthmolen avatar darthmolen commented on August 16, 2024

This. Right Here. I also opened #53 with a similar problem (as you're asking for feedback but they're separate from what he's asking)

from azure-cosmos-dotnet-v3.

j82w avatar j82w commented on August 16, 2024

@TsuyoshiUshio and @darthmolen please take a look at the PR

from azure-cosmos-dotnet-v3.

TsuyoshiUshio avatar TsuyoshiUshio commented on August 16, 2024

@J82 awesome!

from azure-cosmos-dotnet-v3.

j82w avatar j82w commented on August 16, 2024

#90

from azure-cosmos-dotnet-v3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.