Esempio n. 1
0
        public void GetAll_WhenOneStudent_ShouldReturnStatusCode200AndNotNullContent()
        {
            var mockRepository = Mock.Create<IRepository<Student>>();
            var studentToAdd = new Student()
            {
                LastName = "Johnson",
                Age = 20,
                Grade = 12
            };

            IQueryable<Student> studentEntities = (new List<Student>() { studentToAdd }).AsQueryable();

            Mock.Arrange(() => mockRepository.GetAll())
                .Returns(() => studentEntities.AsQueryable());

            var server = new InMemoryHttpServer<Student>("http://localhost/",
                mockRepository);

            var response = server.CreateGetRequest("api/students");

            var str = response.Content.ReadAsStringAsync();
            // The str.Results is an array in string form. When it is empty, it always return "[]" string,
            // meaning that if it has content, its length is greater than 2. The approach shown in the lecture
            // is not working - the Assert always passes.
            Assert.IsTrue(str.Result.Length > 2);
        }
Esempio n. 2
0
        public void GetAll_WhenMultipleStudents_ShouldReturnStatusCode200AndNotNullContent()
        {
            var mockRepository = Mock.Create<IRepository<Student>>();
            var studentToAdd = new Student()
            {
                LastName = "Johnson",
                Age = 20,
                Grade = 12
            };

            var secondStudentToAdd = new Student()
            {
                LastName = "Davis",
                Age = 16,
                Grade = 10
            };

            IQueryable<Student> studentEntities = (new List<Student>() { studentToAdd, secondStudentToAdd }).AsQueryable();

            Mock.Arrange(() => mockRepository.GetAll())
                .Returns(() => studentEntities.AsQueryable());

            var server = new InMemoryHttpServer<Student>("http://localhost/",
                mockRepository);

            var response = server.CreateGetRequest("api/students");

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            var str = response.Content.ReadAsStringAsync();
            Assert.IsTrue(str.Result.Length > 2);
        }
        public void GetAll_WhenMultipleStudentsInRepository_ShouldReturnSameCount()
        {
            var repository = Mock.Create<IRepository<Student>>();
            var firstStudentToAdd = new Student()
            {
                LastName = "First test student"
            };

            var secondStudentToAdd = new Student()
            {
                LastName = "Second test student"
            };

            var thirdStudentToAdd = new Student()
            {
                LastName = "Third test student"
            };

            IQueryable<Student> studentEntities = (new List<Student>() 
            { firstStudentToAdd, secondStudentToAdd, thirdStudentToAdd }).AsQueryable();
            Mock.Arrange(() => repository.GetAll()).Returns(() => studentEntities.AsQueryable());

            var controller = new StudentsController(repository);
            SetupController(controller);

            var response = controller.GetAll();
            List<StudentModel> students = new List<StudentModel>();
            bool gotValue = response.TryGetContentValue(out students);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.IsTrue(gotValue);
            Assert.IsTrue(students.Count == 3);
        }
        public void TestAdd_WithNegativeAge_ShouldThrowException()
        {
            var student = new Student
            {
                FirstName = "John",
                LastName = "Travolta",
                Grade = 11,
                Age = -3
            };

            var postedStudent = this.studentsRepository.Post(student);
        }
        public void TestAdd_WithNullLastName_ShouldThrowException()
        {
            var student = new Student
            {
                FirstName = "John",
                LastName = null,
                Grade = 11,
                Age = 16
            };

            var postedStudent = this.studentsRepository.Post(student);
        }
        public void TestAdd_WithCorrectParameters_ShouldBeAdded()
        {
            var student = new Student
            {
                FirstName = "John",
                LastName = "Travolta",
                Grade = 11,
                Age = 16
            };

            var postedStudent = this.studentsRepository.Post(student);
            Assert.IsTrue(postedStudent.Id > 0);
            var studentFromContext = this.dbContext.Set<Student>().Find(postedStudent.Id);
            Assert.AreEqual(postedStudent.FirstName, studentFromContext.FirstName);
            Assert.AreEqual(postedStudent.LastName, studentFromContext.LastName);
            Assert.AreEqual(postedStudent.Grade, studentFromContext.Grade);
            Assert.AreEqual(postedStudent.Age, studentFromContext.Age);
        }
Esempio n. 7
0
        public void GetById_WhenStudentsExists_ShouldReturnStatusCode200AndSomeContent()
        {
            var mockRepository = Mock.Create<IRepository<Student>>();


            var studentToAdd = new Student()
            {
                LastName = "Johnson",
                Age = 20,
                Grade = 12
            };

            Mock.Arrange(() => mockRepository.GetById(Arg.Is<int>(1)))
                .Returns(() => studentToAdd);

            var server = new InMemoryHttpServer<Student>("http://localhost/",
                mockRepository);

            var response = server.CreateGetRequest("api/students/1");

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            var str = response.Content.ReadAsStringAsync();
            Assert.IsTrue(str.Result.Length > 2);
        }
        public void Post_WhenValid_ShouldReturnAppropriateElement()
        {
            var repository = Mock.Create<IRepository<Student>>();
            var firstStudentToAdd = new Student()
            {
                // Deliberate setting so that the appropriate id can be returned in the 
                // controller.
                Id = 32,
                LastName = "First test student"
            };


            Mock.Arrange(() => repository.Post(Arg.IsAny<Student>())).Returns(() => firstStudentToAdd);

            var controller = new StudentsController(repository);
            SetupController(controller);

            var response = controller.PostStudent(new StudentModel()
            {
                LastName = "John",
                Age = 11,
                Grade = 5
            });

            Student student = new Student();
            bool gotValue = response.TryGetContentValue(out student);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.IsTrue(gotValue);
            Assert.AreEqual(32, student.Id);
        }
        public void TestGetById_ReturnCorrectOptions()
        {
            var student = new Student
            {
                FirstName = "John",
                LastName = "Travolta",
                Grade = 11,
                Age = 16
            };

            var postedStudent = this.studentsRepository.Post(student);
            Assert.IsTrue(postedStudent.Id > 0);
            var studentFromContext = this.studentsRepository.GetById(postedStudent.Id);
            Assert.AreEqual(postedStudent.FirstName, studentFromContext.FirstName);
            Assert.AreEqual(postedStudent.LastName, studentFromContext.LastName);
            Assert.AreEqual(postedStudent.Grade, studentFromContext.Grade);
            Assert.AreEqual(postedStudent.Age, studentFromContext.Age);
        }