Ras*_*nIT 3 c# unit-testing asp.net-core razor-pages
我有一个关于为 Razor Page 与 MVVM 结合编写单元测试的问题。我想OnPostAsync()从 Razor测试一个方法,但是这个方法将调用另外两个需要ClaimsPrincipal用户的方法。但是,我无法User在我的测试中为这些方法提供参数。
我目前的测试方法:
[Test]
public void MakeBookingTest()
{
//Arrange
var optionsbuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsbuilder.UseInMemoryDatabase(databaseName: "TeacherDB");
var _dbContext = new ApplicationDbContext(optionsbuilder.Options);
JsonResult json = new JsonResult(true);
_dbContext.ImportOption.Add(new ImportOption { Id = 1, isUnique = 1, Option = "Teacher" });
_dbContext.SaveChanges();
var mockedUserManager = GetMockUserManager();
var mockedCalendar = new Mock<ICalendarService>();
Booking booking = new Booking {
EventId = "2",
ClassroomId = 3,
BeginTime = DateTime.Now,
EndTime = DateTime.Now,
Description = "fjdkafjal",
Summary = "Test01" };
mockedCalendar.Setup(x => x.CreateEvent(booking, "2", "0863629@hr.nl", false)).Returns("111");
var mockedRoleManager = GetMockRoleManager();
var model = new MakeBookingModel(_dbContext, mockedUserManager.Object, mockedCalendar.Object, mockedRoleManager.Object);
//var controllerContext = new Mock<ControllerContext>();
var result = model.OnPostAsync();
}
Run Code Online (Sandbox Code Playgroud)
我要测试的方法:
[ValidateAntiForgeryToken]
public async Task<IActionResult> OnPostAsync()
{
CurrentRole = await _validation.GetCurrentRole(User);
CurrentUser = await _userManager.GetUserAsync(User);
//(rest of the code...)
Run Code Online (Sandbox Code Playgroud)
_validation.GetCurrentRole 方法:
public async Task<string> GetCurrentRole(ClaimsPrincipal User)
{
ApplicationUser currentUser = await _userManager.GetUserAsync(User);
Task<IList<string>> rolesUser = _userManager.GetRolesAsync(currentUser);
return rolesUser.Result.First();
}
Run Code Online (Sandbox Code Playgroud)
我似乎无法使User在OnPostAsync/GetCurrentRole不为空。
我将如何分配User给这些方法?
User通过 HTTP 上下文设置PageModel's 的一部分PageContext
// Arrange
//...code removed for brevity
//Create test user
var displayName = "User name";
var identity = new GenericIdentity(displayName);
var principle = new ClaimsPrincipal(identity);
// use default context with user
var httpContext = new DefaultHttpContext() {
User = principle
}
//need these as well for the page context
var modelState = new ModelStateDictionary();
var actionContext = new ActionContext(httpContext, new RouteData(), new PageActionDescriptor(), modelState);
var modelMetadataProvider = new EmptyModelMetadataProvider();
var viewData = new ViewDataDictionary(modelMetadataProvider, modelState);
// need page context for the page model
var pageContext = new PageContext(actionContext) {
ViewData = viewData
};
//create model with necessary dependencies
var model = new MakeBookingModel(_dbContext, mockedUserManager.Object, mockedCalendar.Object, mockedRoleManager.Object) {
PageContext = pageContext
};
//Act
//...
Run Code Online (Sandbox Code Playgroud)
在 ASP.NET Core 中参考Razor Pages 单元测试
您正在混合异步和阻塞调用,例如.Result导致GetCurrentRole.
重构为一直异步
public async Task<string> GetCurrentRole(ClaimsPrincipal User) {
ApplicationUser currentUser = await _userManager.GetUserAsync(User);
var rolesUser = await _userManager.GetRolesAsync(currentUser);
return rolesUser.FirstOrDefault();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1092 次 |
| 最近记录: |