public void SetCorrectAnswerTest() { using (var _Transaction = _DbContext.Database.BeginTransaction()) { // Создаем вопрос. DAO.Question _Question = new DAO.Question() { Text = "SetCorrectAnswerTest?" }; _QuestionManagement.CreateQuestion(_Question); _DbContext.SaveChanges(); // Получаем сущность из БД - если не получить, то EF подумает, что надо создать новую. _Question = _DbContext.Questions.Include("Answers").Single(x => x.Text == _Question.Text); // Выбираем ответы для заполнения. DAO.Answer[] _Answers = new DAO.Answer[] { _DbContext.Answers.Single(x => x.Text == "Венера"), _DbContext.Answers.Single(x => x.Text == "Меркурий"), _DbContext.Answers.Single(x => x.Text == "Земля") }; _QuestionManagement.AddAnswer(_Question, _Answers); _DbContext.SaveChanges(); Assert.DoesNotThrow(() => _QuestionManagement.SetCorrectAnswer(_Question, _DbContext.Answers.Single(x => x.Text == "Венера"))); var _UnCorrectAnswer = _DbContext.Answers.Single(x => x.Text == "101010"); Assert.Throws <Exception>(() => _QuestionManagement.SetCorrectAnswer(_Question, _UnCorrectAnswer)); _Transaction.Rollback(); } }
public void SetCorrectAnswerToQuestionTest() { QuestionManagement _QuestionManagement = new QuestionManagement(); AnswerManagement _AnswerManagement = new AnswerManagement(); DAO.Question _Question = _QuestionManagement.GetQuestion(StaticQuestions.Q1); _QuestionManagement.SetCorrectAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A1)); _Question = _QuestionManagement.GetQuestion(StaticQuestions.Q2); _QuestionManagement.SetCorrectAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A6)); _Question = _QuestionManagement.GetQuestion(StaticQuestions.Q3); _QuestionManagement.SetCorrectAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A7)); _Question = _QuestionManagement.GetQuestion(StaticQuestions.Q4); _QuestionManagement.SetCorrectAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A11)); _Question = _QuestionManagement.GetQuestion(StaticQuestions.Q5); _QuestionManagement.SetCorrectAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A13)); _DbContext.SaveChanges(); Assert.True(_Question.Answers.Where(x => x.AnswerId == _AnswerManagement.GetAnswer(StaticAnswers.A13).Id & x.IsCorrect).Count() == 1); }