Пример #1
0
        public async Task GetAddressByIdAsyncShouldReturnAddressByIdSuccessfully()
        {
            var option = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                         .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            var dbContext = new MyCalisthenicAppDbContext(option);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var addressService = new AddressesService(dbContext, mapper);

            var city = new City
            {
                Id       = Guid.NewGuid().ToString(),
                Name     = CityName,
                PostCode = CityPostCode,
            };

            await dbContext.Cities.AddAsync(city);

            await dbContext.SaveChangesAsync();

            var address = new Address
            {
                Id      = AddressId,
                Country = AddressCountryName,
                Street  = AddressStreet,
                CityId  = city.Id,
            };

            await dbContext.Addresses.AddAsync(address);

            await dbContext.SaveChangesAsync();

            var expected = await addressService.GetAddressByIdAsync(AddressId);

            Assert.Equal(expected.Id, address.Id);
        }
Пример #2
0
        public async Task GetAddressByIdAsyncShouldThrowExceptionIfNull()
        {
            var option = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                         .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            var dbContext = new MyCalisthenicAppDbContext(option);

            var addressService = new AddressesService(dbContext, null);

            var city = new City
            {
                Id       = Guid.NewGuid().ToString(),
                Name     = CityName,
                PostCode = CityPostCode,
            };

            await dbContext.Cities.AddAsync(city);

            await dbContext.SaveChangesAsync();

            var address = new Address
            {
                Id      = Guid.NewGuid().ToString(),
                Country = AddressCountryName,
                Street  = AddressStreet,
                CityId  = city.Id,
            };

            await dbContext.Addresses.AddAsync(address);

            await dbContext.SaveChangesAsync();

            var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await addressService.GetAddressByIdAsync(AddressId));

            Assert.IsType <ArgumentNullException>(exception);
        }