I have the code below, I have read Moq and SqlConnection? and How can I stub IDBconnection, but I still have no idea how to mock the following sqlconnection.
public class SqlBulkWriter : ISqlBulkWriter
{
private readonly string _dbConnectionString;;
public SqlBulkWriter(string dbConnectionString)
{
this._dbConnectionString = dbConnectionString;
}
public void EmptyTable(string schema, string tableName)
{
using (var connection = new SqlConnection(this._dbConnectionString))
{
try
{
connection.Open();
using (var truncate = new SqlCommand($"TRUNCATE TABLE [{schema}].[{tableName}] ", connection))
{
truncate.ExecuteNonQuery();
}
}
catch (Exception ex)
{
throw new Exception(ex);
}
finally
{
connection.Close();
}
}
}
}
I'm going to do the unit test for EmptyTable, I think I should mock the sqlconnection firstly? or how do I do the unit test for the EmptyTempTable?
Thank you! Many appreciate!
See Question&Answers more detail:os