I am trying to write the unit test cases for the repositories. I am struggling to mock the dapper using SQL connection.
My method:
public IEnumerable<TestModal> TestMethod(int id)
{
var spName = "sp_Test";
var spParams = new
{
ID= id
};
using (var connection = new SqlConnection(_configuration.GetConnectionString("TestDB")))
{
connection.Open();
return connection.Query<TestModal>(spName, spParams, commandType: CommandType.StoredProcedure);
}
}
I am trying to write the unit test case for the above method.
[TestFixture]
public class TestClassTests
{
private Mock<IConfiguration> _mockConfiguration;
private Mock<SqlConnection> _mockSqlCOnnection;
private ITestRepository _testRepository;
[SetUp]
public void Setup()
{
_mockConfiguration = new Mock<IConfiguration>();
_mockSqlCOnnection = new Mock<SqlConnection>();
_testRepository= new TestRepository(_mockConfiguration.Object);
}
[Test]
public void TestMethodTest()
{
_mockSqlCOnnection.SetupDapper(x => x.Query<TestModal>(It.IsAny<string>(), null, null, true, null, null)).Returns(fakeData);
var result = _testRepository.TestMethod(10);
Assert.IsNotNull(result);
Assert.AreEqual(result.Count(), fakeData.Count());
}
}