Пример #1
0
        public void ReturnResultFromSaveChanges()
        {
            // Arrange
            var expectedResult = new Mock <IDataModifiedResult>();
            var id             = this.Fixture.Create <int>();
            var name           = this.Fixture.Create <string>();
            var descr          = this.Fixture.Create <string>();
            var country        = this.Fixture.Create <string>();
            var brewery        = new Brewery();
            var beerRepo       = new Mock <IEfRepository <Beer> >();

            var repository = new Mock <IEfRepository <Brewery> >();

            repository.Setup(r => r.GetById(id))
            .Returns(brewery);
            repository.Setup(r => r.SaveChanges())
            .Returns(expectedResult.Object);

            var service = new BreweryService(repository.Object, beerRepo.Object);

            // Act
            var result = service.UpdateBrewery(id, name, country, descr);

            // Assert
            Assert.AreSame(expectedResult.Object, result);
        }
Пример #2
0
        public void CallRepositoryUpdateMethodOnceWithCorrectParams()
        {
            // Arrange
            var id      = this.Fixture.Create <int>();
            var name    = this.Fixture.Create <string>();
            var descr   = this.Fixture.Create <string>();
            var country = this.Fixture.Create <string>();
            var brewery = new Brewery();

            var repository = new Mock <IEfRepository <Brewery> >();

            repository.Setup(r => r.GetById(id))
            .Returns(brewery);
            var beerRepo = new Mock <IEfRepository <Beer> >();

            var service = new BreweryService(repository.Object, beerRepo.Object);

            // Act
            var result = service.UpdateBrewery(id, name, country, descr);

            // Assert
            repository.Verify(r => r.Update(brewery), Times.Once);
            Assert.AreSame(name, brewery.Name);
            Assert.AreSame(descr, brewery.Description);
            Assert.AreSame(country, brewery.Country);
        }
Пример #3
0
        public void Return_Correct_Edited_Brewery()
        {
            var options = Utils.GetOptions(nameof(Return_Correct_Edited_Brewery));

            var brewery = new Brewery
            {
                Id          = 1,
                Name        = "Diamond Knot Brewery",
                Description = "America",
                CountryId   = 1
            };

            using (var arrangeContext = new BeerOverflowContext(options))
            {
                arrangeContext.Breweries.Add(brewery);
                arrangeContext.SaveChanges();
            }

            var newName      = "DKB";
            var newDescr     = "USA";
            var newCountryId = 2;

            using (var assertContext = new BeerOverflowContext(options))
            {
                var sut = new BreweryService(assertContext);

                var result = sut.UpdateBrewery(brewery.Id, newName, newDescr, newCountryId);

                Assert.AreEqual(brewery.Id, result.Id);
                Assert.AreEqual(newName, result.Name);
                Assert.AreEqual(newDescr, result.Description);
                Assert.AreEqual(newCountryId, result.CountryId);
            }
        }
Пример #4
0
        public void Throw_When_BreweryNotFound()
        {
            var options = Utils.GetOptions(nameof(Throw_When_BreweryNotFound));

            using (var assertContext = new BeerOverflowContext(options))
            {
                var sut = new BreweryService(assertContext);

                var newName      = "DKB";
                var newDescr     = "USA";
                var newCountryId = 2;
                Assert.ThrowsException <ArgumentNullException>(() => sut.UpdateBrewery(1, newName, newDescr, newCountryId));
            }
        }
        public void CallRepositoryGetByIdMethodOnceWithCorrectParams()
        {
            var id         = this.Fixture.Create <int>();
            var name       = this.Fixture.Create <string>();
            var descr      = this.Fixture.Create <string>();
            var country    = this.Fixture.Create <string>();
            var expected   = new Brewery();
            var repository = new Mock <IRepository <Brewery> >();

            repository.Setup(r => r.GetById(id))
            .Returns(expected);
            var beerRepo = new Mock <IRepository <Beer> >();

            var service = new BreweryService(repository.Object, beerRepo.Object);

            var result = service.UpdateBrewery(id, name, country, descr);

            repository.Verify(r => r.GetById(id), Times.Once);
        }
        public ActionResult Edit(int id, BreweryEdit model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (model.BreweryId != id)
            {
                ModelState.AddModelError("", "Id Mismatch");
                return(View(model));
            }

            if (_service.UpdateBrewery(model))
            {
                TempData["SaveResult"] = "Your brewery was updated.";
                return(RedirectToAction("Index"));
            }

            ModelState.AddModelError("", "Your brewery could not be updated.");
            return(View(model));
        }