I am trying to mock the below method,
public static object ExecuteGetObj(string query)
{
object obj = null;
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Default"].ConnectionString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand(query, connection))
{
obj = cmd.ExecuteScalar();
}
}
return obj;
}
but it is not using the Mock SqlConnection instead it is trying to create the SqlConnection
var command = new Mock<IDbCommand>();
command.Setup(x => x.ExecuteScalar())
.Verifiable();
var connection = new Mock<IDbConnection>();
connection
.Setup(m => m.CreateCommand())
.Returns(command.Object);
connection.Object.Open();
var obj = Common.ExecuteGetObj(sql);
Assert.IsNull(obj);