コード例 #1
0
        public void TestAddInvalidAnimal()
        {
            User user = new User()
            {
                DisplayName = "Some display name"
            };

            Animal animal = new Animal()
            {
                TypeName = "Test animal"
            };

            using (ApiContext context = new ApiContext(dbOptions)) {
                context.Users.Add(user);
                context.Animals.Add(animal);

                context.SaveChanges();
            }

            using (ApiContext context = new ApiContext(dbOptions)) {
                UserAnimalService service = new UserAnimalService(context);

                Assert.Catch <NotFoundException>(() => service.Add(
                                                     new UserAnimal()
                {
                    UserId = user.Id, AnimalId = animal.Id + 1
                }
                                                     ));
            }

            // Make sure no data is in the DB
            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(0, context.UserAnimals.Count());
            }
        }
コード例 #2
0
        public void TestAdd()
        {
            User user = new User()
            {
                DisplayName = "Some display name"
            };

            Animal animal = new Animal()
            {
                TypeName = "Test animal"
            };

            using (ApiContext context = new ApiContext(dbOptions)) {
                context.Users.Add(user);
                context.Animals.Add(animal);

                context.SaveChanges();
            }

            using (ApiContext context = new ApiContext(dbOptions)) {
                UserAnimalService service = new UserAnimalService(context);

                service.Add(new UserAnimal()
                {
                    UserId = user.Id, AnimalId = animal.Id
                });
            }

            // Make sure the user's animal was added to the DB
            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(1, context.UserAnimals.Count());
                Assert.AreEqual(user.Id, context.UserAnimals.First().UserId);
                Assert.AreEqual(animal.Id, context.UserAnimals.First().AnimalId);
            }
        }