I developed an ASP.NET Core 8 MVC project and want to test all action methods using XUnit testing. But when I want to read data from the database using IDataReader, the code throws a NullReferenceException.
This is my XUnit testing code:
public HomeControllerTests()
{
EShopUsers.Add(new EShopUser(1, "test123", "nalin", "Jagath"));
EShopUsers.Add(new EShopUser(2, "[email protected]", "jagath", "tertert"));
}
[Fact()]
public async Task IndexTest()
{
Mock<IDataReader> reader = SetupAndGetMockDataReader(EShopUsers);
Mock<IDbCommand> command = new Mock<IDbCommand>();
Mock<IDbConnection> mockConnection = new Mock<IDbConnection>(MockBehavior.Strict);
mockConnection.Setup(x => x.Open());
mockConnection.Setup(x => x.Dispose());
mockConnection.Setup(x => x.CreateCommand()).Returns(command.Object);
command.Setup(x => x.ExecuteReader()).Returns(reader.Object);
Repositories.EShopUserRepository eShopUserRepository = new Repositories.EShopUserRepository(() => mockConnection.Object);
EShopUserService mockservice = new EShopUserService(eShopUserRepository);
WebApp.Controllers.HomeController homeController = new(mockservice);
var httpActionResult = await homeController.Index();
var actionresult = httpActionResult as ViewResult;
Assert.NotNull(actionresult.Model);
Assert.Equal(((List<EShopUser>)actionresult.Model).Count,2);
}
private Mock<IDataReader> SetupAndGetMockDataReader(List<EShopUser> eShopUsers)
{
var mockreader = new Mock<IDataReader>();
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("ID", typeof(Int32)));
table.Columns.Add(new DataColumn("UserName", typeof(string)));
table.Columns.Add(new DataColumn("FirstName", typeof(string)));
table.Columns.Add(new DataColumn("LastName", typeof(string)));
foreach (EShopUser item in eShopUsers)
{
DataRow row = table.NewRow();
row["ID"] = item.ID;
row["UserName"] = item.UserName;
row["FirstName"] = item.FirstName;
row["LastName"] = item.LastName;
table.Rows.Add(row);
}
for (int i = 0; i < table.Columns.Count; i++)
{
var name = table.Columns[i].ColumnName;
mockreader.Setup(r => r.GetOrdinal(It.Is<string>(n => n == name))).Returns(i);
}
var rowIndex = 0;
mockreader.Setup(r => r.Read()).Returns(() => (rowIndex < table.Rows.Count)).Callback(() => SetupNextRow(mockreader, table, ref rowIndex));
return mockreader;
}
private static void SetupNextRow(Mock<IDataReader> mockDataReader, DataTable table, ref int rowIndex)
{
if (rowIndex >= table.Rows.Count) return;
var row = table.Rows[rowIndex];
for (int columnIndex = 0; columnIndex < row.ItemArray.Length; columnIndex++)
{
DataColumn column = (DataColumn)table.Columns[columnIndex];
switch (column.ColumnName.ToString())
{
case "ID":
mockDataReader.Setup(r => r.GetInt32(It.Is<Int32>(x => x == columnIndex))).Returns((Int32)row[columnIndex]);
break;
case "UserName":
mockDataReader.Setup(r => r.GetString(It.Is<Int32>(x => x == columnIndex))).Returns((string)row[columnIndex]);
break;
case "FirstName":
mockDataReader.Setup(r => r.GetString(It.Is<Int32>(x => x == columnIndex))).Returns((string)row[columnIndex]);
break;
case "LastName":
mockDataReader.Setup(r => r.GetString(It.Is<Int32>(x => x == columnIndex))).Returns((string)row[columnIndex]);
break;
default:
break;
}
}
rowIndex++;
}
HomeController.Index() method is this:
public async Task<IActionResult> Index()
{
List<EShopUser> users = new();
var listPaginated = (List<EShopUser>)await _EShopUserService.GetAllUsersAsync();
foreach (EShopUser item in listPaginated)
{
users.Add(new EShopUser(item.ID, item.UserName, item.FirstName, item.LastName));
}
return View(users);
}
EShopUserService.GetAllUsersAsync() is shown here:
public async Task<object> GetAllUsersAsync()
{
return await _EShopUserRepository.GetAllUsersAsync();
}
And this is the EShopUserRepository.GetAllUsersAsync() method:
private readonly IDbConnection _connection = null;
public EShopUserRepository(Func<IDbConnection> dbConnection)
{
_connection = dbConnection.Invoke();
}
public async Task<object> GetAllUsersAsync()
{
List<EShopUser> users = new List<EShopUser>();
var t = await Task.Run(() =>
{
using (_connection)
{
_connection.Open();
IDbCommand cmd = _connection.CreateCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "GetAllUser";
IDataReader userReader = cmd.ExecuteReader();
while (userReader.Read())
{
users.Add(new EShopUser((int)userReader["ID"], (string)userReader["UserName"], (string)userReader["FirstName"],(string)userReader["LastName"]));
}
}
return Task.FromResult<object>(users);
});
return t;
}
When I debug the GetAllUsersAsync method, the userReader object is null, so it throws a NullReferenceException. What is wrong?
IEnumerable<EshopUser>