Coder Social home page Coder Social logo

mock-dbconnection's Introduction

Fake DbConnection

This project aim to mock the Dbconnection to test your query WITHOUT a database.

I wrote this tool because I found that on many occasions is not a good idea and also is not possible to test your SQL commands against an in-memory provider like SQLite.

Sometimes you want to test: transactions, stored procedures, a DB schema, or a SQL dialect not supported by SQLite, in these cases, is almost impossible or very hard to create a valid SQLite database that can fit your tests.

Another good point for not testing with in-memory DB came from Jimmy Bogard, I suggest you to read this article: Avoid In-Memory Databases for Tests

In the situation where we cannot test with SQLite, what we can do, at least, is to check if the command is correctly created.

How to

  1. Implement the IDbLogger to pass to the instance of MockedDbConnection OR use the ListLogger that log on a List.
  2. Create a new instance the FakedDbConnection
  3. Call your code
  4. Check the result
    // Arrange
    var lst = new ListLogger();
    IDbConnection conn = new MockedDbConnection(lst);

    var sql = "SELECT TITLE FROM CUSTOMERS WHERE ID = @Id AND EMAIL = @EMail";
    var parms = new { Id = 1, EMail = "[email protected]" };

    // Act
    var result = conn.Query<string>(sql, parms);

    // Assert
    Assert.NotNull(result); // return an empty list since theres no DataTable passed to the DbConnection constructor

    // Verify the SQL Command
    var cmd = lst.Log.Where(p => p.Type == DbLogItem.LogType.LogCommand && p.Message == "ExecuteDbDataReader").First();
    Assert.Equal(sql, cmd.Value);

    // Verify the Parameters
    Assert.Equal(parms.EMail, lst.Parameters.First(p => p.ParameterName == nameof(parms.EMail)).Value);
    Assert.Equal(parms.Id, lst.Parameters.First(p => p.ParameterName == nameof(parms.Id)).Value);

Optional

You can also pass a DataTable if you want to mock the results.

Logger

You can write your own IDbLogger implementation to retrive only what you want

    public interface IDbLogger
    {
        void LogParameter(DbParameter parameter);
        
        void LogConnection(string message, string? value = null);
        
        void LogTransaction(string message, string? value = null);

        void LogCommand(string message, string? value = null);
    }

mock-dbconnection's People

Contributors

m4ss1m0g avatar

Watchers

 avatar

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.