I'm trying to do unit testing to my handler database but I got a nullReferenceException when I call my methods.
This is my handler constructor
public SignalsHandler(IConfiguration configuration, IDbConnection connection)
{
_dbConnection = connection;
_settings = configuration.GetSection("SigSettings").Get<SigSettings>();
if (_settings == null)
_settings = new SigSettings();
}
And what I do is to Mock that with this on my testing class:
private Mock<IConfigurationSection> _configSectionMock = new Mock<IConfigurationSection>();
private Mock<IConfiguration> _configMock = new Mock<IConfiguration>();
private Mock<IDbConnection> _connection = new Mock<IDbConnection>();
private readonly SignalsHandler _handler;
public SignalsTest()
{
_configMock.Setup(x => x.GetSection(It.IsAny<string>())).Returns(_configSectionMock.Object);
_handler = new SignalsHandler(_configMock.Object, _connection.Object);
}
So far so good but when I try to implement some testing it doesn't work. For example, a simple test:
public void CanGetAllSignals()
{
var result = _handler.GetSignals();
Assert.IsNotNull(result);
}
I call my .GetSignals()
method with my Mock handler. This is the code:
public async Task<List<SignalsDTO>> GetSignals()
{
var res = new List<SignalsDTO>();
var sigList = await _dbConnection.QueryAsync<SigSignal>(_settings.Signals);
foreach (var s in sigList)
res.Add(new SignalsDTO()
{
IDTag = s.IDTag,
Name = s.Name
});
return res;
}
But when I debug my testing method I got:
NullReferenceException:'Object reference not set to an instance of an object.'
at this line:
var sigList = await _dbConnection.QueryAsync<SigSignal>(_settings.Signals);
I suppose this is an error with my mock configuration at my test constructor but I'm not sure about how can I fix this.