예제 #1
0
        public async Task AddStudent()
        {
            _schoolServiceMock.Setup(d => d.AddStudentAsync(It.IsAny <Student>())).Returns(Task.Run(() => new Student {
                StudentId = 1, StudentName = "Valentin"
            }));

            var httpContextMock  = new Mock <HttpContextBase>();
            var httpReguestMock  = new Mock <HttpRequestBase>();
            var httpResponseMock = new Mock <HttpResponseBase>();

            httpContextMock.SetupGet(c => c.Response).Returns(httpResponseMock.Object);
            httpContextMock.SetupGet(c => c.Request).Returns(httpReguestMock.Object);

            var schoolController = new StudentController(_schoolServiceMock.Object);

            schoolController.ControllerContext = new ControllerContext(httpContextMock.Object, new RouteData(), schoolController);
            schoolController.Url = new UrlHelper(new RequestContext(schoolController.HttpContext, new RouteData()));

            var result = (await schoolController.AddNewStudent(new StudentViewModel {
                Id = 0, Name = "test"
            }, @"Student\Index")) as RedirectToRouteResult;

            Assert.AreEqual("Student", result.RouteValues["controller"]);
            Assert.AreEqual("Index", result.RouteValues["action"]);
        }