예제 #1
0
        public void Add()
        {
            //mock
            int studentCount = 10;
            IList<Student> students = Builder<Student>.CreateListOfSize(studentCount).Build();
            Db.Setup(x => x.Students).Returns(students);
            InitializeServer();

            //api call
            var entityToAdd = new Student {Name = "Dipon"};
            ApiResponseTmpl<Student> responseTmplObj;

            var client = new HttpClient(Server);
            HttpRequestMessage request = CreateRequest("student/create", "application/json", HttpMethod.Post, JsonConvert.SerializeObject(entityToAdd)); //important; new{Entity: entityToAdd} wasn't working as expected
            using (HttpResponseMessage response = client.SendAsync(request).Result)
            {
                responseTmplObj = JsonConvert.DeserializeObject<ApiResponseTmpl<Student>>(response.Content.ReadAsStringAsync().Result);
            }

            //at db
            Assert.AreEqual(11, students.Count);
            Assert.AreEqual(entityToAdd.Name, students.Last().Name);
            Assert.IsTrue(students.Last().IsActive);

            //returned
            Student addedEntity = responseTmplObj.Data;
            Assert.AreEqual(11, addedEntity.Id);
            Assert.AreEqual(entityToAdd.Name, addedEntity.Name); //passed by json
            Assert.IsTrue(addedEntity.IsActive);
        }
예제 #2
0
        public void Add()
        {
            //mock
            int studentCount = 10;
            IList<Student> students = Builder<Student>.CreateListOfSize(studentCount).Build();
            Db.Setup(x => x.Students).Returns(students);
            InitializeServer();

            //api
            var entityToAdd = new Student {Name = "Dipon"};
            JObject response = JsonApi.Add(entityToAdd);
            Student addedEntity = response.ToObject<ApiResponseTmpl<Student>>().Data;

            //at db
            Assert.AreEqual(11, students.Count);
            Assert.AreEqual(entityToAdd.Name, students.Last().Name);
            Assert.IsTrue(students.Last().IsActive);

            //returned
            Assert.AreEqual(11, addedEntity.Id);
            Assert.AreEqual(entityToAdd.Name, addedEntity.Name); //passed by json
            Assert.IsTrue(addedEntity.IsActive);
        }
예제 #3
0
        public void Replace()
        {
            //mock
            const int studentId = 1;
            IList<Student> students = Builder<Student>.CreateListOfSize(2).All().With(x => x.IsActive = true).Build();
            Db.Setup(x => x.Students).Returns(students);
            InitializeServer();

            //api
            var entity = new Student {Name = "Dipon", IsActive = false};
            JObject response = JsonApi.Replace(studentId, entity);
            Student updatedEntity = response.ToObject<ApiResponseTmpl<Student>>().Data;

            //at db
            Assert.AreEqual(entity.Name, students.First().Name);
            Assert.AreEqual(entity.IsActive, students.First().IsActive);

            //returned
            Assert.AreEqual(studentId, updatedEntity.Id);
            Assert.AreEqual(entity.Name, updatedEntity.Name);
            Assert.AreEqual(entity.IsActive, updatedEntity.IsActive);
        }
예제 #4
0
 public JObject Replace(int id, Student entity)
 {
     return Put(String.Format("student/update/{0}", id), entity);
 }
예제 #5
0
 public JObject Add(Student entity)
 {
     return Post("student/create", entity);
 }
예제 #6
0
        public void Replace()
        {
            //mock
            const int studentId = 1;
            IList<Student> students = Builder<Student>.CreateListOfSize(2).All().With(x => x.IsActive = true).Build();
            Db.Setup(x => x.Students).Returns(students);
            InitializeServer();

            //api call
            var entity = new Student {Name = "Dipon", IsActive = false};
            ApiResponseTmpl<Student> responseTmplObj;

            var client = new HttpClient(Server);
            HttpRequestMessage request = CreateRequest(String.Format("student/update/{0}", studentId), "application/json", HttpMethod.Put, JsonConvert.SerializeObject(entity)); //important; new{Entity: entityToAdd} wasn't working as expected
            using (HttpResponseMessage response = client.SendAsync(request).Result)
            {
                responseTmplObj = JsonConvert.DeserializeObject<ApiResponseTmpl<Student>>(response.Content.ReadAsStringAsync().Result);
            }

            //at db
            Assert.AreEqual(entity.Name, students.First().Name);
            Assert.AreEqual(entity.IsActive, students.First().IsActive);

            //returned
            Student updatedEntity = responseTmplObj.Data;
            Assert.AreEqual(studentId, updatedEntity.Id); //passed by url
            Assert.AreEqual(entity.Name, updatedEntity.Name); //passed by json
            Assert.AreEqual(entity.IsActive, updatedEntity.IsActive); //passed by json
        }