Это мой код тестирования XUNIT:
Код: Выделить всё
public HomeControllerTests()
{
EShopUsers.Add(new EShopUser(1, "test123", "nalin", "Jagath"));
EShopUsers.Add(new EShopUser(2, "semyid@gmail.com", "jagath", "tertert"));
}
[Fact()]
public async Task IndexTest()
{
Mock reader = SetupAndGetMockDataReader(EShopUsers);
Mock command = new Mock();
Mock mockConnection = new Mock(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)actionresult.Model).Count,2);
}
private Mock SetupAndGetMockDataReader(List eShopUsers)
{
var mockreader = new Mock();
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(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 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(x => x == columnIndex))).Returns((Int32)row[columnIndex]);
break;
case "UserName":
mockDataReader.Setup(r => r.GetString(It.Is(x => x == columnIndex))).Returns((string)row[columnIndex]);
break;
case "FirstName":
mockDataReader.Setup(r => r.GetString(It.Is(x => x == columnIndex))).Returns((string)row[columnIndex]);
break;
case "LastName":
mockDataReader.Setup(r => r.GetString(It.Is(x => x == columnIndex))).Returns((string)row[columnIndex]);
break;
default:
break;
}
}
rowIndex++;
}
< /code>
HomeController.Index()Код: Выделить всё
public async Task Index()
{
List users = new();
var listPaginated = (List)await _EShopUserService.GetAllUsersAsync();
foreach (EShopUser item in listPaginated)
{
users.Add(new EShopUser(item.ID, item.UserName, item.FirstName, item.LastName));
}
return View(users);
}
< /code>
EShopUserService.GetAllUsersAsync()Код: Выделить всё
public async Task GetAllUsersAsync()
{
return await _EShopUserRepository.GetAllUsersAsync();
}
< /code>
And this is the EShopUserRepository.GetAllUsersAsync()Код: Выделить всё
private readonly IDbConnection _connection = null;
public EShopUserRepository(Func dbConnection)
{
_connection = dbConnection.Invoke();
}
public async Task GetAllUsersAsync()
{
List users = new List();
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(users);
});
return t;
}
< /code>
When I debug the GetAllUsersAsyncПодробнее здесь: https://stackoverflow.com/questions/796 ... it-testing