Ejemplo n.º 1
0
        public void Add_ValidEntity_Must_Not_Return_Null()
        {
            //
            // Arrange
            //
            var studentList = GetStudentList();
            var context = GetMyUniDbContext(studentList);
            var mockedRepository = new Mock<GenericRepository<Student>>(context);
            mockedRepository.Setup(x => x.Add(It.IsAny<Student>())).Returns((Student s) =>
            {
                if (s == null)
                {
                    return null;
                }

                studentList.Add(s);
                return s;
            });
            var newStudent = new Student {FirstName = "D"};
            //
            // Act
            //
            var student = mockedRepository.Object.Add(newStudent);
            //
            // Assert
            //
            Assert.AreEqual(4, studentList.Count);
            Assert.IsNotNull(student, "The student does not exist");
        }
Ejemplo n.º 2
0
        public static Student ToEntity(this StudentViewModel studentViewModel)
        {
            var student = new Student
            {
                Id = studentViewModel.Id,
                FirstName = studentViewModel.FirstName,
                LastName = studentViewModel.LastName,
                EnrolledDate = studentViewModel.EnrolledDate,
                Enrollments = studentViewModel.Enrollments == null ? new Collection<Enrollment>() :
                    new Collection<Enrollment>(studentViewModel.Enrollments.Select(x => x.ToEntity()).ToList())
            };

            return student;
        }
Ejemplo n.º 3
0
        public void Update_Non_Existing_Entity_Must_Not_Be_Updated()
        {
            var studentList = GetStudentList();
            var context = GetMyUniDbContext(studentList);
            var mockedRepository = new Mock<GenericRepository<Student>>(context);
            mockedRepository.Setup(x => x.Update(It.IsAny<Student>())).Callback((Student student) =>
            {
                var findIndex = studentList.FindIndex(x => x.Id == student.Id);
                if (findIndex < 0)
                {
                    return;
                }

                studentList[findIndex] = student;
            });
            var studentToUpdate = new Student{FirstName = "AAA"};

            mockedRepository.Object.Update(studentToUpdate);

            Assert.AreEqual(studentList[0].FirstName, "A");
        }
Ejemplo n.º 4
0
        public void Delete_Non_Existing_Id_Must_Not_Affect_Collection()
        {
            var studentList = GetStudentList();
            var context = GetMyUniDbContext(studentList);
            var mockedRepository = new Mock<GenericRepository<Student>>(context);
            mockedRepository.Setup(x => x.Delete(It.IsAny<int>())).Callback((int id) =>
            {
                studentList.Remove(studentList.FirstOrDefault(x => x.Id == id));
            });

            var studentToRemove = new Student { Id = 999 };

            mockedRepository.Object.Delete(studentToRemove.Id);

            Assert.AreEqual(3, studentList.Count);
        }
Ejemplo n.º 5
0
        public void Delete_Non_Existing_Entity_Must_Not_Affect_Collection()
        {
            var studentList = GetStudentList();
            var context = GetMyUniDbContext(studentList);
            var mockedRepository = new Mock<GenericRepository<Student>>(context);
            mockedRepository.Setup(x => x.Delete(It.IsAny<Student>())).Callback((Student s) =>
            {
                studentList.Remove(s);
            });

            var studentToRemove = new Student{FirstName = "AAA"};

            mockedRepository.Object.Delete(studentToRemove);

            Assert.AreEqual(3, studentList.Count);
        }