public async Task OnPageAsync() { #region Arrange var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase("InMemoryDb"); var mockDb = new Mock <ApplicationDbContext>(optionsBuilder.Options); var expectedUsers = ApplicationDbContext.GetSeedMentor(); var expectedUser = expectedUsers.FirstOrDefault(); var username = expectedUser.UserName; mockDb.Setup(db => db.GetMentorAsync(username)) .Returns(Task.FromResult(expectedUser)); var strategicDomains = ApplicationDbContext.GetSeedStrategicDomains(); var pageModel = new StrategicDomainModel(mockDb.Object); pageModel.Username = username; pageModel.StrategicDomain = strategicDomains.FirstOrDefault(); #endregion #region Act var result = await pageModel.OnPostAsync(); #endregion #region Assert Assert.IsType <RedirectToPageResult>(result); #endregion }
public async Task OnPageAsync_InvalidModelState() { #region Arrange var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase("InMemoryDb"); var mockDb = new Mock <ApplicationDbContext>(optionsBuilder.Options); var expectedUsers = ApplicationDbContext.GetSeedMentor(); var expectedUser = expectedUsers.FirstOrDefault(); var username = expectedUser.UserName; mockDb.Setup(db => db.GetMentorAsync(username)) .Returns(Task.FromResult(expectedUser)); var strategicDomain = ApplicationDbContext.GetSeedStrategicDomains(); var pageModel = new StrategicDomainModel(mockDb.Object) { Username = username, StrategicDomain = strategicDomain.FirstOrDefault() }; pageModel.ModelState.AddModelError("This is an test error", "Error Message!"); #endregion #region Act var result = await pageModel.OnPostAsync(); #endregion #region Assert Assert.IsType <RedirectResult>(result); var pageResult = result as RedirectResult; var url = pageResult.Url; Assert.Contains( "Error", url); #endregion }