public void TestPostAnimal() { _context = DbContextMocker.GetAnimalsGameContext(Guid.NewGuid().ToString()); _service = new AnimalsGameService(_context, null); _controller = new AnimalController(_context, _service); Animal newAnimal = new Animal { AnimalId = 123, Type = AnimalType.SMALL, Name = "Penguin", Happiness = Constants.NEUTRAL, Hunger = Constants.NEUTRAL, UserId = 0 // No user assigned }; _controller.AddAnimal(newAnimal); List <Animal> animals = _controller.GetAnimals(); Assert.NotNull(animals); Assert.Equal(4, animals.Count); _context.Database.EnsureDeleted(); }
public static void Seed(this AnimalsGameContext context) { context.Animals.Add(new Animal { AnimalId = 1, Type = AnimalType.SMALL, Name = "Bear", Happiness = Constants.NEUTRAL, Hunger = Constants.NEUTRAL, UserId = 0 // No user assigned }); context.Animals.Add(new Animal { AnimalId = 2, Type = AnimalType.MEDIUM, Name = "Walrus", Happiness = Constants.NEUTRAL, Hunger = Constants.NEUTRAL, UserId = 0 // No user assigned }); context.Animals.Add(new Animal { AnimalId = 3, Type = AnimalType.XLARGE, Name = "Hamster", Happiness = Constants.NEUTRAL, Hunger = Constants.NEUTRAL, UserId = 0 // No user assigned }); context.SaveChanges(); }
public static AnimalsGameContext GetAnimalsGameContext(string dbName) { var options = new DbContextOptionsBuilder <AnimalsGameContext>() .UseInMemoryDatabase(databaseName: dbName) .Options; var dbContext = new AnimalsGameContext(options); dbContext.Seed(); return(dbContext); }
public void TestGetAllAnimals() { _context = DbContextMocker.GetAnimalsGameContext(Guid.NewGuid().ToString()); _service = new AnimalsGameService(_context, null); _controller = new AnimalController(_context, _service); List <Animal> animals = _controller.GetAnimals(); Assert.NotNull(animals); Assert.Equal(3, animals.Count); _context.Database.EnsureDeleted(); }
public void TestGetAnimal() { _context = DbContextMocker.GetAnimalsGameContext(Guid.NewGuid().ToString()); _service = new AnimalsGameService(_context, null); _controller = new AnimalController(_context, _service); var result = _controller.GetAnimal(1); Assert.NotNull(result); Assert.Equal("Bear", result.Value.Name); _context.Database.EnsureDeleted(); }
public void TestFeedAnimal() { _context = DbContextMocker.GetAnimalsGameContext(Guid.NewGuid().ToString()); _service = new AnimalsGameService(_context, null); _controller = new AnimalController(_context, _service); _controller.FeedAnimal(1); var firstResult = _controller.GetAnimal(1); Console.Write(firstResult.Value.Hunger); Assert.Equal(5, firstResult.Value.Hunger); _controller.FeedAnimal(1); var secondResult = _controller.GetAnimal(1); Assert.Equal(0, secondResult.Value.Hunger); _context.Database.EnsureDeleted(); }
private void ChangeValues(object state) { while (true) { Thread.Sleep(sleepTime); using (var context = new AnimalsGameContext(_options)) { Animal animal = context.Animals.Find(_animalId); if (animal != null) { animal.Happiness -= _animalMetric; animal.Hunger += _animalMetric; context.Entry(animal).State = EntityState.Modified; context.SaveChanges(); } } } }
public UsersGameService(AnimalsGameContext context) { _context = context; }
public UserController(AnimalsGameContext context, IUsersGameService service) { _context = context; _service = service; }
public AnimalsGameService(AnimalsGameContext context, DbContextOptions <AnimalsGameContext> options) { _context = context; _options = options; }