提问人:Ntavass 提问时间:11/9/2023 更新时间:11/9/2023 访问量:54
使用 MSTest 在 C# 中模拟筛选的数据
Mocking Filtered Data in C# with MSTest
问:
我是 C# 的新手,我正在尝试使用 MSTest 模拟 C# 单元测试中的过滤数据,用于从数据库中检索学生数据的服务。下面的代码成功地模拟了整个学生数据列表,但未能根据指定的条件正确筛选数据。
public class NewRequestServiceTests
{
private readonly NewRequestService _newRequestService;
private readonly Mock<DatabaseContext> _databaseContext;
private StudentData _studentDataDE;
private StudentData _studentDataFR ;
public NewRequestServiceTests()
{
_databaseContext = new Mock<DatabaseContext>();
_newRequestService = new NewRequestService(_databaseContext.Object);
}
[TestInitialize]
public void Setup()
{
_InitializeTestDataBeforeTests(); //Initialize dummy data
_databaseContext.Setup(dc => dc.StudentData).ReturnsDbSet(new List<StudentData>
{
_studentDataDE, // initialized in the _InitializeTestDataBeforeTests() method
_studentDataFR // initialized in the _InitializeTestDataBeforeTests() method
});
}
[TestMethod]
public async Task GetNewRequest_ValidResponse()
{
var studentId = 1;
var response = await _newRequestService.GetNewRequest(studentId , new List<int>());
// Asserts
// ...
}
}
在另一种方法中,使用以下代码筛选数据:
var studentDataList = await _databaseContext.StudentData
.Include(e => e.ApplicableEducationalLegislationBasis)
.Where(sd => sd.StudentId == id
&& sd.ApplicableEducationalLegislationBasis.IsActive == true
&& sd.AssessingSchoolAuthorityId != userAuthority.Code)
.ToListAsync();
此代码不会引发错误,但会返回一个空列表。有人有任何想法或相关话题吗?
先谢谢你。
答: 暂无答案
评论
DatabaseContext