Ejemplo n.º 1
0
        public void Exams_Controller_Test_On_CreateModel_With_Invalid_Model()
        {
            //Arrange
            Guid     id           = new Guid("f616cc8c-2223-4145-b7d0-232a1f6f0795");
            string   title        = "TestT";
            string   observations = "TestF";
            DateTime dateStart    = DateTime.Now;
            DateTime dateEnd      = DateTime.Now.AddDays(1);

            Exam expectedExams = new Exam(title, observations, dateStart, dateEnd);

            expectedExams.Id = id;

            ExamModel expectedModel = new ExamModel();

            expectedModel.Title = " ";
            var repo = Substitute.For <IRepository>();
            var sut  = new ExamsController(repo);

            repo.Create(expectedExams);

            //Act
            sut.ModelState.AddModelError("Title", "Title Required");
            var actual = sut.Create(expectedModel).Result;

            //Assert
            Assert.IsInstanceOfType(actual, typeof(ViewResult));
        }
        public async Task Redirect_To_Index_After_Successful_Creation()
        {
            // Arrange
            var fixture = new Fixture();

            fixture.Behaviors.OfType <ThrowingRecursionBehavior>().ToList()
            .ForEach(b => fixture.Behaviors.Remove(b));
            fixture.Behaviors.Add(new OmitOnRecursionBehavior());

            var context               = GetFakeContext();
            var examDbSetMock         = new Mock <DbSet <Exam> >();
            var examQuestionDbSetMock = new Mock <DbSet <ExamQuestion> >();

            context.Questions.AddRange(fixture.CreateMany <Question>(7).ToList());
            context.Exams         = examDbSetMock.Object;
            context.ExamQuestions = examQuestionDbSetMock.Object;
            context.SaveChanges();

            var controller = new ExamsController(context, GetUserManagerMock().Object);
            var viewModel  = fixture.Create <ExamCreationViewModel>();

            viewModel.QuestionAmount = 3;

            // Act
            var result = await controller.Create(viewModel);

            // Assert
            examDbSetMock.Verify(dbSet => dbSet.AddAsync(It.IsNotNull <Exam>(), default), Times.Once);
            examQuestionDbSetMock.Verify(dbSet => dbSet.AddRangeAsync(It.IsNotNull <ICollection <ExamQuestion> >(), default), Times.Once);
            Assert.NotNull(result);
            Assert.IsAssignableFrom <RedirectToActionResult>(result);
            Assert.Equal("Exams", ((RedirectToActionResult)result).ControllerName);
            Assert.Equal("Index", ((RedirectToActionResult)result).ActionName);
        }
Ejemplo n.º 3
0
        public void Create_ReturnsAView()
        {
            var controller = new ExamsController();

            var result = controller.Create() as ViewResult;

            Assert.IsNotNull(result);
        }
Ejemplo n.º 4
0
        public void Create_ReturnsAView()
        {
            var controller = new ExamsController();

            var result = controller.Create() as ViewResult;

            Assert.IsNotNull(result);
        }
Ejemplo n.º 5
0
        public void CreatePost_AddsQuestionToTheRepository()
        {
            var mockQuestionsRepository = MockRepository.GenerateMock<QuestionsRepository>();
            var stubQuestion = MockRepository.GenerateStub<Question>();

            var controller = new ExamsController(mockQuestionsRepository, null, null);
            controller.Create(stubQuestion);

            mockQuestionsRepository.AssertWasCalled(m => m.Add(stubQuestion));
        }
Ejemplo n.º 6
0
        public void CreatePost_AddsQuestionToTheRepository()
        {
            var mockQuestionsRepository = MockRepository.GenerateMock <QuestionsRepository>();
            var stubQuestion            = MockRepository.GenerateStub <Question>();

            var controller = new ExamsController(mockQuestionsRepository, null, null);

            controller.Create(stubQuestion);

            mockQuestionsRepository.AssertWasCalled(m => m.Add(stubQuestion));
        }
Ejemplo n.º 7
0
        public void CreatePost_ReturnsRedirectOnSuccess()
        {
            var stubQuestionsRepository = MockRepository.GenerateMock<QuestionsRepository>();

            var controller = new ExamsController(stubQuestionsRepository, null, null);
            ActionResult result = controller.Create(null);

            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            var redirectResult = (RedirectToRouteResult)result;
            Assert.AreEqual("Index", redirectResult.RouteValues["action"]);
        }
Ejemplo n.º 8
0
        public void Exams_Controller_Test_On_Create()
        {
            //Arrange
            var repo = Substitute.For <IRepository>();
            var sut  = new ExamsController(repo);

            //Act
            var actual = sut.Create();

            //Assert
            Assert.IsInstanceOfType(actual, typeof(ViewResult));
        }
Ejemplo n.º 9
0
        public void CreatePost_ReturnsRedirectOnSuccess()
        {
            var stubQuestionsRepository = MockRepository.GenerateMock <QuestionsRepository>();

            var          controller = new ExamsController(stubQuestionsRepository, null, null);
            ActionResult result     = controller.Create(null);

            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            var redirectResult = (RedirectToRouteResult)result;

            Assert.AreEqual("Index", redirectResult.RouteValues["action"]);
        }
        public async Task Return_Bad_Request_Result_When_ModelState_Is_Invalid_On_Create_Post()
        {
            // Arrange
            var controller = new ExamsController(GetFakeContext(), GetUserManagerMock().Object);

            controller.ModelState.AddModelError("Name", "Required");
            var viewModel = new ExamCreationViewModel();

            // Act
            var result = await controller.Create(viewModel);

            // Assert
            var badRequestResult = Assert.IsType <BadRequestObjectResult>(result);

            Assert.IsType <SerializableError>(badRequestResult.Value);
        }
        public async Task Return_Bad_Request_Result_When_QuestionAmount_Is_Higher_Than_Available_Questions_Amount()
        {
            // Arrange
            var context = GetFakeContext();

            context.Questions.RemoveRange(context.Questions.ToList());

            var controller = new ExamsController(context, GetUserManagerMock().Object);
            var viewModel  = new ExamCreationViewModel();

            viewModel.QuestionAmount = int.MaxValue;

            // Act
            var result = await controller.Create(viewModel);

            // Assert
            var badRequestResult = Assert.IsType <BadRequestObjectResult>(result);

            Assert.IsType <SerializableError>(badRequestResult.Value);
        }