1

I have following method:

public async Task SaveToLogAsync(string clientId)
{
    try
    {
        using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            using (var cmd = new SqlCommand())
            {
                cmd.Connection = conn;
                cmd.CommandText = @"INSERT INTO TerminalsActivityLog(ClientId, LogTime) VALUES(@clientId, @logTime)";
                cmd.Parameters.Add("@clientId", SqlDbType.VarChar).Value = clientId;
                DateTime dt = DateTime.Now;
                cmd.Parameters.Add("@logTime", SqlDbType.DateTime).Value = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0 , 0 , dt.Kind);

                await conn.OpenAsync();
                await cmd.ExecuteNonQueryAsync();
            }
        }
    }          
    catch (Exception )
    {
    }
}

How can I write an unit test without saving to database? The best option would be something like InMemory db in connectionString for EF. Or should I save to db and then delete saved data?

Sasha
  • 833
  • 1
  • 20
  • 40

1 Answers1

1

Unit tests will have it's own configuration. What you could do is define another database in the connectionstring in the configuration for the Unit test project.

In the unit test project create a setup method that creates that database and sets it up with all the necessary structures. Then create a cleanup method that drops the database.

lobiZoli
  • 199
  • 9