I used Dapper.Contrib in my Asp.Net Core Web API project. I encountered a problem while writing a test with xUnit in this project. For example, here is my method that adds records to my data layer.
public async Task<bool> AddAsync(User entity)
{
await using var connection = _dbConnection.CreateDbConnection();
await connection.OpenAsync();
return await connection.InsertAsync(entity) > 0;
}
My xUnit method that I try to write according to this method is below.
[Fact]
public void AddAsync_Should_Return_As_Expected()
{
var connection = new Mock<DbConnection>();
//Arrange
_userDbConnection.Setup(u => u.CreateDbConnection()).Returns(connection.Object);
//Act
var result = _sut.AddAsync(_user).GetAwaiter().GetResult();
//Assert
//Assert.Equal(result,actual);
}
When I run this test method, I get object not set error in 'return await connection.InsertAsync(entity) > 0;' line.
What exactly is my fault?