Пример #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"};
            Student addedEntity = null;

            var client = new HttpClient(Server);
            HttpRequestMessage request = CreateRequest("api/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)
            {
                addedEntity = JsonConvert.DeserializeObject<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
            Assert.AreEqual(11, addedEntity.Id);
            Assert.AreEqual(entityToAdd.Name, addedEntity.Name); //passed by json
            Assert.IsTrue(addedEntity.IsActive);
        }
Пример #2
0
        public void Add()
        {
            var entityToAdd = new Student {Name = "Dipon"};
            Student addedEntity = null;

            var client = new HttpClient(_server);
            HttpRequestMessage request = CreateRequest("api/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)
            {
                addedEntity = JsonConvert.DeserializeObject<Student>(response.Content.ReadAsStringAsync().Result);
            }

            Assert.Less(0, addedEntity.Id);
            Assert.AreEqual(entityToAdd.Name, addedEntity.Name); //passed by json
            Assert.IsTrue(addedEntity.IsActive);
        }
Пример #3
0
        public void Replace()
        {
            const int studentId = 1;
            var entity = new Student {Name = "Dipon", IsActive = false};
            Student updatedEntity = null;

            var client = new HttpClient(_server);
            HttpRequestMessage request = CreateRequest(String.Format("api/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)
            {
                updatedEntity = JsonConvert.DeserializeObject<Student>(response.Content.ReadAsStringAsync().Result);
            }

            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
        }
Пример #4
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};
            Student updatedEntity = null;
            var client = new HttpClient(Server);
            HttpRequestMessage request = CreateRequest(String.Format("api/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)
            {
                updatedEntity = JsonConvert.DeserializeObject<Student>(response.Content.ReadAsStringAsync().Result);
            }

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

            //returned
            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
        }