Exemplo n.º 1
0
        public async Task GetAddressesByCityShouldReturnOkIfFoundInDatabase()
        {
            // Arrange
            var city    = "Sopot";
            var address = new Address()
            {
                Id             = 1,
                Country        = "Polska",
                City           = city,
                Street         = "Ciasna",
                BuildingNumber = "1",
                FlatNumber     = "69",
                PostalCode     = "80-765"
            };

            mockRepo.Setup(x => x.Address).Returns(_addressRepoMock.Object);
            _addressRepoMock.Setup(x => x.GetAddressByCity(city)).ReturnsAsync(new[] { address });

            // Act
            var result = await _sut.GetAddressesByCity(city);

            // Assert
            Assert.IsType <OkObjectResult>(result);
            Assert.IsNotType <NotFoundResult>(result);
        }
        public void TestGetAddressCallsDataSource()
        {
            var dataSrc   = new Mock <IAddressDataSource>();
            var logger    = new Mock <ILogger <AddressController> >();
            var addresses = CreateAddresses();

            dataSrc.Setup(d => d.GetAllAddresses()).Returns(addresses).Verifiable();
            var target  = new AddressController(dataSrc.Object, logger.Object);
            var results = target.GetAddressesByCity();

            var london = results.Where(r => r.CityGroupName == "London").FirstOrDefault();
            var rome   = results.Where(r => r.CityGroupName == "Rome").FirstOrDefault();
            var nyc    = results.Where(r => r.CityGroupName == "New York").FirstOrDefault();
            var chi    = results.Where(r => r.CityGroupName == "Chicago").FirstOrDefault();

            Assert.IsNotNull(london);
            Assert.IsNotNull(rome);
            Assert.IsNotNull(nyc);
            Assert.IsNotNull(chi);
            Assert.AreEqual(london.Addresses.Count(), 1);
            Assert.AreEqual(rome.Addresses.Count(), 1);
            Assert.AreEqual(nyc.Addresses.Count(), 2);
            Assert.AreEqual(chi.Addresses.Count(), 1);

            var bruno   = rome.Addresses.Where(a => a.LastName == "Bruno").FirstOrDefault();
            var brown   = london.Addresses.Where(a => a.LastName == "Brown").FirstOrDefault();
            var francis = nyc.Addresses.Where(a => a.LastName == "Francis").FirstOrDefault();
            var davey   = nyc.Addresses.Where(a => a.LastName == "Davey").FirstOrDefault();
            var king    = chi.Addresses.Where(a => a.LastName == "King").FirstOrDefault();

            Assert.IsNotNull(bruno);
            Assert.IsNotNull(brown);
            Assert.IsNotNull(francis);
            Assert.IsNotNull(davey);
            Assert.IsNotNull(king);

            dataSrc.VerifyAll();
        }