public void GetStudents()
        {
            //var school = new School().AddSchoolToDatabase(
            var mockRepository = Mock.Create<DbStudentRepository>();
            bool added = false;

            var student = new Student()
                .AddFirstName("ivan")
                .AddLastName("petkov")
                .AddAge(8)
                .AddGrade(2)
                .AddSchoolId(1);

            //IQueryable

            Mock.Arrange(() => mockRepository.All())
                .DoInstead(() => added = true)
                .Returns((new Student[] {student, student}).AsQueryable());

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

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

            Assert.AreEqual(responce.StatusCode, HttpStatusCode.OK);
            Assert.IsTrue(added);
        }
        // POST api/student
        public HttpResponseMessage Post(Student value)
        {
            var created = this.studentsRepository.Add(value);

            var responce = Request.CreateResponse<Student>(HttpStatusCode.Created, created);
            var resourceLink = Url.Link("DefaultApi", new { id = created.Id });

            responce.Headers.Location = new Uri(resourceLink);

            return responce;
        }
예제 #3
0
 public static Student CreateStudentToReturn(Student found)
 {
     var student = new Student()
     {
         Id = found.Id,
         FirstName = found.FirstName,
         LastName = found.LastName,
         Age = found.Age,
         Grade = found.Grade,
         Marks = found.Marks,
         SchoolId = found.SchoolId
     };
     return student;
 }
        public void InsertStudentGrade()
        {
            var school = new School().AddSchoolToDatabase(dbContext);

            var student = new Student()
                .AddFirstName("Pesho")
                .AddGrade(5)
                .AddSchoolId(school.SchoolId);

            var created = this.studentsRepository.Add(student);
            var found = this.dbContext.Set<Student>().Find(created.Id);

            Assert.AreEqual(student.FirstName, found.FirstName);
            Assert.AreEqual(student.Grade, found.Grade);
        }
        public void PostEmptyStudent()
        {
            //var school = new School().AddSchoolToDatabase(
            var mockRepository = Mock.Create<DbStudentRepository>();

            var student = new Student();

            Mock.Arrange(() => mockRepository.Add(Arg.IsAny<Student>()))
                .Throws(new DbEntityValidationException());

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

            var responce = server.CreatePostRequest("api/students", JsonConvert.SerializeObject(student));

            Assert.AreEqual((int)responce.StatusCode, 500);
        }
        // PUT api/student/5
        public HttpResponseMessage Put(int id, Student value)
        {
            if (id != value.Id || value == null)
            {
                return new HttpResponseMessage(HttpStatusCode.BadRequest);
            }

            var updated = this.studentsRepository.Update(id, value);

            var responce = Request.CreateResponse<Student>(HttpStatusCode.Created, updated);
            var resourceLink = Url.Link("DefaultApi", new { id = updated.Id });

            responce.Headers.Location = new Uri(resourceLink);

            return responce;

        }
        public void InsertBasicStudent()
        {
            var school = new School().AddSchoolToDatabase(dbContext);

            var student = new Student()
                .AddFirstName("Pesho")
                .AddLastName("Ivanov")
                .AddSchoolId(school.SchoolId);

            var created = this.studentsRepository.Add(student);
            var found = this.dbContext.Set<Student>().Find(created.Id);

            Assert.AreEqual(student.FirstName, found.FirstName);
            Assert.AreEqual(student.LastName, found.LastName);
            Assert.AreEqual(student.SchoolId, school.SchoolId);
            Assert.IsTrue(student.Id > 0);
            Assert.IsTrue(student.SchoolId > 0);
        }
        public void GetMarkWithMock()
        {
            var repository = Mock.Create<DbStudentRepository>();
            bool itemIsAdded = false;

            var student = new Student()
                .AddFirstName("I")
                .AddLastName("Mock")
                .AddId(0).AddMarks("math", 5.5, 3) ;

            Mock.Arrange(() => repository.Add(Arg.IsAny<Student>()))
                .DoInstead(() => { itemIsAdded = true; })
                .Returns(student);

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

            controller.Post(student);

            Assert.IsTrue(itemIsAdded == true);
        }
        public void PostStudent()
        {
            //var school = new School().AddSchoolToDatabase(
            var mockRepository = Mock.Create<DbStudentRepository>();
            bool added = false;

            var student = new Student()
                .AddFirstName("ivan")
                .AddLastName("petkov")
                .AddAge(8)
                .AddGrade(2)
                .AddSchoolId(1);

            Mock.Arrange(() => mockRepository.Add(Arg.IsAny<Student>()))
                .DoInstead(() => added = true)
                .Returns(student);

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

            var responce = server.CreatePostRequest("api/students", JsonConvert.SerializeObject(student));

            Assert.AreEqual(responce.StatusCode, HttpStatusCode.Created);
            Assert.IsTrue(added);
        }
        public void PostStudentWithoutSchool()
        {
            //var school = new School().AddSchoolToDatabase(
            var mockRepository = Mock.Create<DbStudentRepository>();

            var student = new Student()
                .AddFirstName("ivan")
                .AddLastName("petkov")
                .AddAge(8)
                .AddGrade(2);

            Mock.Arrange(() => mockRepository.Add(Arg.IsAny<Student>()))
                .Throws(new DbEntityValidationException());

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

            var responce = server.CreatePostRequest("api/students", JsonConvert.SerializeObject(student));

            Assert.AreEqual(responce.StatusCode, HttpStatusCode.InternalServerError);
        }
        public void InsertStudentSchoolCheck()
        {
            var school = new School().AddSchoolToDatabase(dbContext, "Mrete", "mladost");

            var student = new Student()
                .AddFirstName("Pesho")
                .AddSchoolId(school.SchoolId);

            var created = this.studentsRepository.Add(student);
            var found = this.dbContext.Set<Student>().Find(created.Id);

            Assert.AreEqual(student.SchoolId, school.SchoolId);
            Assert.IsTrue(student.Id > 0);
            Assert.IsTrue(student.SchoolId > 0);
            Assert.AreEqual(student.School.Name, school.Name);
            Assert.AreEqual(student.School.Location, school.Location);
        }
 public override Student Update(int id, Student entity)
 {
     throw new NotImplementedException();
 }
 public override Student Add(Student entity)
 {
     students.Add(entity);
     return entity;
 }
        public void InsertSchoolWithStudent()
        {
            string schoolName = "TVU";
            string studentFirstName = "Ivan4o";
            string studentLastName = "Kalpazan4o";

            var student = new Student()
                .AddFirstName(studentFirstName)
                .AddLastName(studentLastName);

            var school = new School()
                .AddName(schoolName)
                .AddStudent(student);

            var created = this.schoolRepository.Add(school);
            var found = dbContext.Set<School>().Find(created.SchoolId);
            var studentInSchool = found.Students.First();

            Assert.AreEqual(schoolName, found.Name);
            Assert.AreEqual(student.FirstName, studentInSchool.FirstName);
            Assert.AreEqual(student.LastName, studentInSchool.LastName);
            Assert.AreEqual(student.Id, studentInSchool.Id);
            Assert.AreEqual(school.SchoolId, studentInSchool.SchoolId);
        }
예제 #15
0
 public static School AddStudent(this School sc, Student stud)
 {
     sc.Students.Add(stud);
     return sc;
 }
        public void PostStudent()
        {
            var repository = new FakeStudentRepository(new DbContext("JustMock sucks"));

            var student = new Student();

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

            controller.Post(student);

            Assert.IsTrue(repository.students.Count == 1);
        }