コード例 #1
0
        public async Task UpdateNotExistingStadium()
        {
            var countriesList = new List <Country> {
                new Country {
                    Id = 1, Name = "Spain", Code = "SP"
                }
            };
            var stadiumsList = new List <Stadium>();

            var mockCountryRepo = new Mock <IRepository <Country> >();

            var mockStadiumRepo = new Mock <IRepository <Stadium> >();

            mockStadiumRepo.Setup(r => r.All()).Returns(stadiumsList.AsQueryable());

            var stadiumService = new StadiumService(mockStadiumRepo.Object, mockCountryRepo.Object);

            var updatedViewModel = new StadiumViewModel
            {
                Id        = 1,
                Name      = "Santiago Bernabeu",
                CountryId = 1
            };

            await Assert.ThrowsAsync <Exception>(() => stadiumService.UpdateAsync(updatedViewModel));
        }
コード例 #2
0
        public async Task SaveAndUpdateStadiumWithNameOfAnotherdExistingStadium()
        {
            var countriesList = new List <Country> {
                new Country {
                    Id = 1, Name = "Spain", Code = "SP"
                }
            };
            var stadiumsList = new List <Stadium>();
            var id           = 1;

            var mockCountryRepo = new Mock <IRepository <Country> >();

            mockCountryRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => countriesList.FirstOrDefault(c => c.Id == id));

            var mockStadiumRepo = new Mock <IRepository <Stadium> >();

            mockStadiumRepo.Setup(r => r.All()).Returns(stadiumsList.AsQueryable());
            mockStadiumRepo.Setup(r => r.AddAsync(It.IsAny <Stadium>())).Callback <Stadium>(stadium => stadiumsList.Add(new Stadium
            {
                Id       = id++,
                Name     = stadium.Name,
                Capacity = stadium.Capacity,
                Country  = stadium.Country
            }));

            var stadiumService = new StadiumService(mockStadiumRepo.Object, mockCountryRepo.Object);

            var firstStadiumViewModel = new StadiumViewModel
            {
                Name      = "Santiago Bernabeu",
                CountryId = 1
            };

            var secondStadiumViewModel = new StadiumViewModel
            {
                Name      = "Camp Nou",
                CountryId = 1
            };

            await stadiumService.CreateAsync(firstStadiumViewModel);

            await stadiumService.CreateAsync(secondStadiumViewModel);

            var secondUpdatedViewModel = new StadiumViewModel
            {
                Id        = 2,
                Name      = "Santiago Bernabeu",
                CountryId = 1
            };

            await Assert.ThrowsAsync <Exception>(() => stadiumService.UpdateAsync(secondUpdatedViewModel));
        }
コード例 #3
0
        public async Task SaveAndUpdateStadium()
        {
            var countriesList = new List <Country> {
                new Country {
                    Id = 1, Name = "Spain", Code = "SP"
                }
            };
            var stadiumsList = new List <Stadium>();

            var mockCountryRepo = new Mock <IRepository <Country> >();

            mockCountryRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => countriesList.FirstOrDefault(c => c.Id == id));

            var mockStadiumRepo = new Mock <IRepository <Stadium> >();

            mockStadiumRepo.Setup(r => r.All()).Returns(stadiumsList.AsQueryable());
            mockStadiumRepo.Setup(r => r.AddAsync(It.IsAny <Stadium>())).Callback <Stadium>(stadium => stadiumsList.Add(new Stadium
            {
                Id       = 1,
                Name     = stadium.Name,
                Capacity = stadium.Capacity,
                Country  = stadium.Country
            }));

            var stadiumService = new StadiumService(mockStadiumRepo.Object, mockCountryRepo.Object);

            var stadiumViewModel = new StadiumViewModel
            {
                Name      = "Santiago Bernabeu",
                CountryId = 1
            };

            await stadiumService.CreateAsync(stadiumViewModel);

            var updatedViewModel = new StadiumViewModel
            {
                Id        = 1,
                Name      = "Santiago Bernabeu",
                CountryId = 1
            };

            await stadiumService.UpdateAsync(updatedViewModel);

            var savedStadium = stadiumService.Get(1);

            Assert.Equal(1, savedStadium.Id);
            Assert.Equal("Santiago Bernabeu", savedStadium.Name);
        }