Writing Xunit test cases in C#

I am kind of learning to write unit test cases and I am using Xunit framework. I have a scenario where I would like to write a test case to test different scenario in my cosmos db emulator. To do that I am trying to create a database, container and insert few test data in my cosmos db emulator and then write my facts and also delete it once test cases are completed...below is the code which I figured out from internet, would like to know if I am doing it correctly... and where can I start writing my test cases.

    namespace Project.Tests
{
    public class DatabaseFixture : IDisposable
    {
        private static readonly string CosmosEndpoint = "https://localhost:8081";
        private static readonly string EmulatorKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
        private static readonly string DatabaseId = "Recordings";
        private static readonly string RecordingCollection = "testdata";

    public DatabaseFixture()
    {
        var client = new DocumentClient( new Uri( CosmosEndpoint ), EmulatorKey,
            new ConnectionPolicy
            {
                ConnectionMode = ConnectionMode.Direct,
                ConnectionProtocol = Protocol.Tcp

            } );
        var databaseCreationResult = client.CreateDatabaseAsync( new Database { Id = DatabaseId } ).Result;
        var collectionCreationResult = client.CreateDocumentCollectionAsync( UriFactory.CreateDatabaseUri( DatabaseId ),
            new DocumentCollection { Id = RecordingCollection } ).Result;
        var recData = new Recordings { Id = "Test" };

        
        var itemResult = client
            .CreateDocumentAsync(
                UriFactory.CreateDocumentCollectionUri( DatabaseId, RecordingCollection ), recData )
            .Result;            
        var document = client
            .ReadDocumentAsync(
                UriFactory.CreateDocumentUri( DatabaseId, RecordingCollection, itemResult.Resource.Id ) )
            .Result;            
        Recordings site = (dynamic)document.Resource;
    }

    public void Dispose()
    {
        // ... clean up test data from the database ...
        throw new NotImplementedException();
    }
}

public class Recordings
{
    public string Id { get; set; }
}

public class MyDatabaseTests : IClassFixture<DatabaseFixture>
{
    DatabaseFixture fixture;

    public MyDatabaseTests( DatabaseFixture fixture )
    {
        this.fixture = fixture;
    }

    // ... write tests, using fixture.Db to get access to the database server ...
}

}



Read more here: https://stackoverflow.com/questions/67396496/writing-xunit-test-cases-in-c-sharp

Content Attribution

This content was originally published by ZZZSharePoint at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: