public void GetBySubjectAndMark_WithMarkValueAboveThanExisting_ShouldReturnEmptyCollection() { var unitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; IList<Student> studentEntities = new List<Student>(); studentEntities.Add(studentEntity); string subject = "math"; double mark = 6.2; Mock.Arrange(() => unitOfWork.StudentsRepository.GetBySubjectAndMark(subject, mark)) .Returns(() => studentEntities .Where(st => st.Marks.Any(m => m.Subject == subject && m.Value > mark)) .AsQueryable<Student>()); var studentsController = new StudentsController(unitOfWork); SetupController(studentsController); var httpResponseWithStudentModels = studentsController.Get(subject, mark); var studentModels = httpResponseWithStudentModels.Content .ReadAsAsync<IQueryable<StudentModel>>().Result; Assert.AreEqual(httpResponseWithStudentModels.StatusCode, HttpStatusCode.OK); Assert.AreEqual(0, studentModels.Count()); }
public void GetByValidID_ShouldReturnCorrectStudent() { var unitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; IList<Student> studentEntities = new List<Student>(); studentEntities.Add(studentEntity); int id = 1; // Database IDs starts with 1 id--; Mock.Arrange(() => unitOfWork.StudentsRepository.Get(id)) .Returns(() => id >= 0 ? studentEntities[id] : null); var studentsController = new StudentsController(unitOfWork); SetupController(studentsController); var httpResponseWithStudentModels = studentsController.Get(id); var studentModel = httpResponseWithStudentModels.Content.ReadAsAsync<StudentModel>().Result; Assert.AreEqual(httpResponseWithStudentModels.StatusCode, HttpStatusCode.OK); // Check personal information Assert.AreEqual(studentEntity.FirstName, studentModel.FirstName); Assert.AreEqual(studentEntity.LastName, studentModel.LastName); Assert.AreEqual(studentEntity.Age, studentModel.Age); Assert.AreEqual(studentEntity.Grade, studentModel.Grade); Assert.AreEqual(school.Name, studentModel.School.Name); Assert.AreEqual(school.Location, studentModel.School.Location); // Check marks Assert.AreEqual(studentEntity.Marks.First().Subject, studentModel.Marks.First().Subject); Assert.AreEqual(studentEntity.Marks.First().Value, studentModel.Marks.First().Value); }
public void GetBySubjectAndMark_WithSingleStudent_ShouldReturnCorrectStudent() { var unitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; IList<Student> studentEntities = new List<Student>(); studentEntities.Add(studentEntity); string subject = "math"; double mark = 5.9; Mock.Arrange(() => unitOfWork.StudentsRepository.GetBySubjectAndMark(subject, mark)) .Returns(() => studentEntities .Where(st => st.Marks.Any(m => m.Subject == subject && m.Value > mark)) .AsQueryable<Student>()); var studentsController = new StudentsController(unitOfWork); SetupController(studentsController); var httpResponseWithStudentModels = studentsController.Get(subject, mark); var studentModel = httpResponseWithStudentModels.Content .ReadAsAsync<IQueryable<StudentModel>>().Result.First(); Assert.AreEqual(httpResponseWithStudentModels.StatusCode, HttpStatusCode.OK); // Check personal information Assert.AreEqual(studentEntity.FirstName, studentModel.FirstName); Assert.AreEqual(studentEntity.LastName, studentModel.LastName); Assert.AreEqual(studentEntity.Age, studentModel.Age); Assert.AreEqual(studentEntity.Grade, studentModel.Grade); Assert.AreEqual(school.Name, studentModel.School.Name); Assert.AreEqual(school.Location, studentModel.School.Location); // Check marks Assert.AreEqual(studentEntity.Marks.First().Subject, studentModel.Marks.First().Subject); Assert.AreEqual(studentEntity.Marks.First().Value, studentModel.Marks.First().Value); }
public void GetByInvalidId_ShoulReturnInternalServerErrorResponse() { var unitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; IList<Student> studentEntities = new List<Student>(); studentEntities.Add(studentEntity); int id = -1; // Database IDs starts with 1 id--; Mock.Arrange(() => unitOfWork.StudentsRepository.Get(id)) .Returns(() => id >= 0 ? studentEntities[id] : null); var studentsController = new StudentsController(unitOfWork); SetupController(studentsController); var httpResponseWithStudentModels = studentsController.Get(id); Assert.AreEqual(httpResponseWithStudentModels.StatusCode, HttpStatusCode.InternalServerError); }