public void DeleteMethodShouldReturnProperAnswerModel() { var user3 = new ApplicationUser() { Id = "3asdfasdfgahasre", UserName = "******" }; var cat = new Category() { CategoryId = 10, AgeRating=AgeRating.Mature, Name="Category 1" }; List<ApplicationUser> users = new List<ApplicationUser> { new ApplicationUser() { Id = "1asdfasdfgahasre" } }; var quest = new Question() { QuestionId = 10, Creator = user3, Category = cat, Title = "Q Title", Text = "Q Text", IsApproved = true, DateAsked = DateTime.Now }; var list = new List<Answer>(); for (int i = 1; i < 6; i++) { list.Add(new Answer() { AnswerId = i, Comment = "Comment" + i, User = new ApplicationUser() { Id = "1asdfasdfgahasre", UserName = "******" }, DateAnswered = DateTime.Now, Question = quest, IsRead = true, IsReported=true, IsVoted=false, }); } var ansRepoMock = new Mock<IRepository<Answer>>(); ansRepoMock.Setup(ur => ur.All()).Returns(list.AsQueryable()); var uofMock = new Mock<IUowData>(); uofMock.Setup(x => x.Answers).Returns(() => { return ansRepoMock.Object; }); var controller = new AnswersController(uofMock.Object); var viewResult = controller.Delete(2) as ViewResult; var model = viewResult.Model; var modelAsAM = model as AnswerView; Assert.IsNotNull(modelAsAM, "Delete action returns null."); Assert.IsNotNull(model, "The model is null."); }
public ActionResult Create(CategoryModel category) { if (ModelState.IsValid) { Category newCategory = new Category(); newCategory.Name = category.Name; newCategory.AgeRating = category.AgeRating; this.Data.Categories.Add(newCategory); this.Data.SaveChanges(); return RedirectToAction("Index"); } return View(category); }
public void DeleteMethodShouldReturnProperCategoryModel() { var cat = new Category() { CategoryId = 10, AgeRating=AgeRating.Mature, Name="Category 1" }; var ctegoryRepoMock = new Mock<IRepository<Category>>(); ctegoryRepoMock.Setup(ur => ur.GetById(10)).Returns(cat); var uofMock = new Mock<IUowData>(); uofMock.Setup(x => x.Categories).Returns(() => { return ctegoryRepoMock.Object; }); var controller = new CategoriesController(uofMock.Object); var modelState = controller.ModelState.IsValid ; var viewResult = controller.Delete(10) as ViewResult; var model = viewResult.Model; var modelAsIenum = model as CategoryModel; Assert.IsNotNull(viewResult, "Delete action returns null."); Assert.IsNotNull(model, "The model is null."); }