0

I am using Snowflake Connector to communicate my .NET Core application with Snowflake. My requirement is to write MS unit tests for the repository layer

My repository class looks like:

using (IDbConnection conn = new SnowflakeDbConnection())
            {
                conn.ConnectionString = ConnectionString;
                conn.Open();
                 var cmd = conn.CreateCommand(); 
               cmd.CommandText = $"SELECT * from TABLENAME";
                var reader = cmd.ExecuteReader();
                 while (reader.Read())
                 {
                            --do some operation
                 }
                conn.Close();
            }

Do I need to perform unit tests on the original Snowflake account or is there any approach like In-Memory?

Could you please help me with this issue?

SAREKA AVINASH
  • 437
  • 8
  • 15

1 Answers1

0

Avoid tightly coupling to implementation details. Focus on implementing the desired logic and abstracting away any 3rd party concerns.

Create a simple connection factory abstraction

public interface IDbConnectionFactory {
    IDbConnection CreateConnection();
}

with a simple implementation.

public class SnowflakeDbConnectionFactory: IDbConnectionFactory {
    public IDbConnection CreateConnection() {
        return new SnowflakeDbConnection();
    }
}

The repository would explicitly depend on the factory abstraction and make use of that

using (IDbConnection conn = factory.CreateConnection()) {
    conn.ConnectionString = ConnectionString;
    conn.Open();
    IDbCommand cmd = conn.CreateCommand(); 
    cmd.CommandText = $"SELECT * from TABLENAME";
    var reader = cmd.ExecuteReader();
    while (reader.Read()) {
        //--do some operation
    }
    conn.Close();
}

This will allow the abstractions to be mocked/stubbed when testing the code in isolation. (ie: unit tests)

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • could you please tell me how to get the reference of a factory and ConnectionString in the using statement and this will be executed as in-memory only? – SAREKA AVINASH Jun 24 '20 at 15:47