public void GetByIdShouldThrowExceptionOnEmptyId() { // Setup var mock = new Mock <IDbService>(); mock.Setup(p => p.LoadRecordById <QuestionModel>(COLLECTION, null)).Returns(new QuestionModel()); var questionsAdmin = new QuestionsAdmin(mock.Object); // Assertion Assert.Throws <ArgumentNullException>(() => questionsAdmin.GetById(null)); }
public ActionResult questions(int?page) { var db = new ApplicationDbContext(); var currentUserId = User.Identity.GetUserId(); var currentUser = db.Users.Find(currentUserId); if (!User.IsInRole("AdminRole")) { return(RedirectToAction("index", "Home")); } var list = new List <QuestionsAdmin>(); foreach (Question q in db.questions.Include("Author").Where(_q => _q.IsDeleted == false)) { QuestionsAdmin qa = new QuestionsAdmin(); qa.AuthorName = q.Author.UserName; qa.questionId = q.Id; if (q.QuestionText.Length > 40) { var str = q.QuestionText.Substring(0, 35); str += "..."; qa.questionText = str; } else { qa.questionText = q.QuestionText; } var avc = 0; foreach (Answer _a in db.answers.Include("ConcreteQuestion").Where(a => a.IsDeleted == false).Where(a => a.ConcreteQuestion.Id == q.Id)) { var count = (from v in db.votes where v.votedAnswer.Id == _a.Id select v).Count(); avc += count; } qa.votesCount = avc; qa.reportsCount = db.reports.Include("ReportedQuestion").Where(a => a.ReportedQuestion.Id == q.Id).Count(); list.Add(qa); } list = list.OrderByDescending(a => a.votesCount).ToList(); int pageNumber = (page ?? 1); int pageSize = 10; PagedList <QuestionsAdmin> model = new PagedList <QuestionsAdmin>(list, pageNumber, pageSize); return(View(model)); }
public void GetByIdShouldReturnValue() { // Setup var question = DbSeed.GetQuestions().First(); question.Id = Guid.NewGuid().ToString(); var mock = new Mock <IDbService>(); mock.Setup(p => p.LoadRecordById <QuestionModel>(COLLECTION, question.Id)).Returns(question); var questionsAdmin = new QuestionsAdmin(mock.Object); // Execution var result = questionsAdmin.GetById(question.Id); // Assertion Assert.Equal(question.Id, result.Id); }
public void SaveOrUpdateShouldExcecuteUpsertRecordOnce() { //Setup var question = DbSeed.GetQuestions().First(); var id = Guid.NewGuid().ToString(); var mock = new Mock <IDbService>(); mock.Setup(p => p.InsertRecord(COLLECTION, question)); mock.Setup(p => p.UpsertRecord(COLLECTION, id, question)); var questionsAdmin = new QuestionsAdmin(mock.Object); // Execution questionsAdmin.SaveOrUpdate(question, id); // Assertion mock.Verify(x => x.InsertRecord(COLLECTION, question), Times.Never); mock.Verify(x => x.UpsertRecord(COLLECTION, id, question), Times.Once); }