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 CreateQuestion(QuestionModel questionModel) { ValidateNewQuestiionInput(questionModel); this.LoadDropDownModelsInViewBag(); if (ModelState.IsValid) { var userName = this.User.Identity.Name; var currentUser = this.Data.Users.All().FirstOrDefault(u => u.UserName == userName); var newQuestion = new Question() { Text = questionModel.QuestionText, CategoryId = questionModel.CategoryId, Title = questionModel.QuestionTitle, TargetedMaxAge = questionModel.TargetedMaxAge, TargetedMinAge = questionModel.TargetedMinAge, Creator = currentUser, DateAsked = DateTime.Now }; if (questionModel.TargetedGender < 0) { newQuestion.TargetedGender = null; } else { newQuestion.TargetedGender = (Gender)questionModel.TargetedGender; } var user = this.Data.Users.All().FirstOrDefault(u => u.UserName == userName); newQuestion.Creator = user; user.Points -= PointsForQuestion; this.Data.Questions.Add(newQuestion); this.Data.SaveChanges(); this.ViewBag.ShowConfirm = true; this.ViewBag.PointsForQuestion = PointsForQuestion; return View("AskQuestion"); } JavaScriptSerializer js = new JavaScriptSerializer(); string json = js.Serialize(questionModel); ViewBag.JsonModel = json; return View("AskQuestion", questionModel); }