Beispiel #1
0
        public void Get_ReturnsGroupedValues()
        {
            // arrange
            var options = new DbContextOptionsBuilder <AddressBookDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddressBook")
                          .Options;

            using (var context = new AddressBookDbContext(options))
            {
                context.AddressBook.Add(new AddressBook {
                    FirstName = "Jane", LastName = "Jones", StreetAddress = "Test Rd 1", City = "Rome", Country = "Italy"
                });
                context.AddressBook.Add(new AddressBook {
                    FirstName = "Tim", LastName = "Smith", StreetAddress = "Test Rd 2", City = "Paris", Country = "France"
                });
                context.AddressBook.Add(new AddressBook {
                    FirstName = "John", LastName = "Doe", StreetAddress = "Test Rd 3", City = "Paris", Country = "France"
                });
                context.SaveChanges();
            }

            using (var context = new AddressBookDbContext(options))
            {
                // act
                var            controller = new AddressBookController(context);
                OkObjectResult result     = controller.Get() as OkObjectResult;
                var            content    = result.Value as IDictionary <string, List <AddressBook> >;

                // assert
                Assert.Equal(3, context.AddressBook.Count());
                Assert.IsType <OkObjectResult>(result);
                Assert.Equal(200, result.StatusCode);
                Assert.Equal(2, content.Count());
            }
        }
Beispiel #2
0
        public AddressBookControllerTests()
        {
            _controllerExceptionHandlerMock = new Mock <ControllerExceptionHandler>();
            _requestContextMock             = new Mock <RequestContext>();
            _httpRequestBaseMock            = new Mock <HttpRequestBase>();


            _httpContextBaseMock = new Mock <HttpContextBase>();
            _httpContextBaseMock.SetupGet(x => x.Request).Returns(_httpRequestBaseMock.Object);

            _exceptionContext = new ExceptionContext
            {
                HttpContext    = _httpContextBaseMock.Object,
                RequestContext = _requestContextMock.Object
            };

            _contentLoaderMock      = new Mock <IContentLoader>();
            _addressBookServiceMock = new Mock <IAddressBookService>();
            _addressBookServiceMock.Setup(x => x.CanSave(It.IsAny <AddressModel>())).Returns(true);

            _contentLoaderMock.Setup(c => c.Get <StartPage>(ContentReference.StartPage)).Returns(new StartPage());

            _localizationService = new MemoryLocalizationService();
            _localizationService.AddString(CultureInfo.CreateSpecificCulture("en"), "/AddressBook/Form/Error/ExistingAddress", "error");

            _subject = new AddressBookController(_contentLoaderMock.Object, _addressBookServiceMock.Object, _localizationService, _controllerExceptionHandlerMock.Object);
            _subject.ControllerContext = new ControllerContext(_httpContextBaseMock.Object, new RouteData(), _subject);
        }
        public static void RegisterConfiguration()
        {
            var storage        = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings[DefaultCloudConfigurationName].ConnectionString);
            var initialization = Task.WhenAll(
                BlobController.OneTimeInitializeAsync(storage),
                InboxController.OneTimeInitializeAsync(storage),
                WindowsPushNotificationClientController.OneTimeInitializeAsync(storage),
                AddressBookController.OneTimeInitializeAsync(storage));

            initialization.Wait();
        }
        public AddressBookControllerTest()
        {
            ///Arrange

            var _mapper = new Mock <IMapper>();

            _mapper.Setup(m => m.Map <NewAddressVm, Address>(It.IsAny <NewAddressVm>())).Returns(new Address());

            var _logger = new Mock <ILogger <AddressBookController> >();

            _service = new AddressBookServiceFake();

            _controller = new AddressBookController(_logger.Object, _mapper.Object, _service);
        }
Beispiel #5
0
        public async Task Get_AddressBook_success()
        {
            //Arrange

            var catalogContext = new AddressBookContext(_dbOptions);

            //Act
            var testController = new AddressBookController(catalogContext);
            var actionResult   = await testController.GetAddressBookAsync().ConfigureAwait(false);

            //Assert
            Assert.IsType <ActionResult <List <CountryAddressBook> > >(actionResult);
            var result = Assert.IsAssignableFrom <List <CountryAddressBook> >(actionResult.Value);

            Assert.Equal(3, result.Count);
        }
        public async Task RemoveAddressAsync_ShouldBeOkObjectResult()
        {
            // Arrange
            var user = new User
            {
                Id = Guid.NewGuid()
            };

            var address = new Address(
                UserId.FromGuid(user.Id),
                Country.Canada,
                "Line1",
                null,
                "City",
                "State",
                "PostalCode");

            TestMock.UserService.Setup(userManager => userManager.GetUserAsync(It.IsAny <ClaimsPrincipal>())).ReturnsAsync(user).Verifiable();

            TestMock.AddressService.Setup(addressService => addressService.FindUserAddressAsync(It.IsAny <User>(), It.IsAny <AddressId>()))
            .ReturnsAsync(address)
            .Verifiable();

            TestMock.AddressService.Setup(addressService => addressService.RemoveAddressAsync(It.IsAny <Address>()))
            .ReturnsAsync(DomainValidationResult <Address> .Succeeded(address))
            .Verifiable();

            var controller = new AddressBookController(TestMock.UserService.Object, TestMock.AddressService.Object, TestMapper);

            // Act
            var result = await controller.RemoveAddressAsync(address.Id);

            // Assert
            result.Should().BeOfType <OkObjectResult>();

            TestMock.UserService.Verify(userManager => userManager.GetUserAsync(It.IsAny <ClaimsPrincipal>()), Times.Once);

            TestMock.AddressService.Verify(addressService => addressService.FindUserAddressAsync(It.IsAny <User>(), It.IsAny <AddressId>()), Times.Once);

            TestMock.AddressService.Verify(addressService => addressService.RemoveAddressAsync(It.IsAny <Address>()), Times.Once);
        }
Beispiel #7
0
        public void Get_ReturnsEmptyObject()
        {
            // arrange
            var options = new DbContextOptionsBuilder <AddressBookDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddressBook")
                          .Options;

            using (var context = new AddressBookDbContext(options))
            {
                // act
                var            controller = new AddressBookController(context);
                OkObjectResult result     = controller.Get() as OkObjectResult;
                var            content    = result.Value as IDictionary <string, List <AddressBook> >;

                // assert
                Assert.Empty(context.AddressBook);
                Assert.IsType <OkObjectResult>(result);
                Assert.Equal(200, result.StatusCode);
                Assert.Empty(content);
            }
        }
        public async Task FetchAddressBookAsync_ShouldBeOkObjectResult()
        {
            // Arrange
            var user = new User
            {
                Id = Guid.NewGuid()
            };

            var address = new Address(
                UserId.FromGuid(user.Id),
                Country.Canada,
                "Line1",
                null,
                "City",
                "State",
                "PostalCode");

            var addressBook = new List <Address>
            {
                address
            };

            TestMock.UserService.Setup(userManager => userManager.GetUserAsync(It.IsAny <ClaimsPrincipal>())).ReturnsAsync(user).Verifiable();

            TestMock.AddressService.Setup(addressService => addressService.FetchAddressBookAsync(It.IsAny <User>())).ReturnsAsync(addressBook).Verifiable();

            var controller = new AddressBookController(TestMock.UserService.Object, TestMock.AddressService.Object, TestMapper);

            // Act
            var result = await controller.FetchAddressBookAsync();

            // Assert
            result.Should().BeOfType <OkObjectResult>();

            result.As <OkObjectResult>().Value.Should().BeEquivalentTo(TestMapper.Map <ICollection <AddressDto> >(addressBook));

            TestMock.UserService.Verify(userManager => userManager.GetUserAsync(It.IsAny <ClaimsPrincipal>()), Times.Once);

            TestMock.AddressService.Verify(addressService => addressService.FetchAddressBookAsync(It.IsAny <User>()), Times.Once);
        }
        public async Task FetchAddressBookAsync_ShouldBeNoContentResult()
        {
            // Arrange
            var user = new User();

            TestMock.UserService.Setup(userManager => userManager.GetUserAsync(It.IsNotNull <ClaimsPrincipal>())).ReturnsAsync(user).Verifiable();

            TestMock.AddressService.Setup(addressService => addressService.FetchAddressBookAsync(It.IsAny <User>()))
            .ReturnsAsync(new Collection <Address>())
            .Verifiable();

            var controller = new AddressBookController(TestMock.UserService.Object, TestMock.AddressService.Object, TestMapper);

            // Act
            var result = await controller.FetchAddressBookAsync();

            // Assert
            result.Should().BeOfType <NoContentResult>();

            TestMock.UserService.Verify(userManager => userManager.GetUserAsync(It.IsAny <ClaimsPrincipal>()), Times.Once);

            TestMock.AddressService.Verify(addressService => addressService.FetchAddressBookAsync(It.IsAny <User>()), Times.Once);
        }
        public void Setup()
        {
            _controllerExceptionHandler = new Mock<ControllerExceptionHandler>();
            _requestContext = new Mock<RequestContext>();
            _httpRequestBase = new Mock<HttpRequestBase>();

            _httpContextBase = new Mock<HttpContextBase>();
            _httpContextBase.Setup(x => x.Request).Returns(_httpRequestBase.Object);

            _exceptionContext = new ExceptionContext
            {
                HttpContext = _httpContextBase.Object,
                RequestContext = _requestContext.Object
            };

            _contentLoaderMock = new Mock<IContentLoader>();
            _addressBookServiceMock = new Mock<IAddressBookService>();
            _addressBookServiceMock.Setup(x => x.CanSave(It.IsAny<Address>())).Returns(true);

            _contentLoaderMock.Setup(c => c.Get<StartPage>(ContentReference.StartPage)).Returns(new StartPage());

            _localizationService = new MemoryLocalizationService();
            _localizationService.AddString(CultureInfo.CreateSpecificCulture("en"), "/AddressBook/Form/Error/ExistingAddress", "error");

            _subject = new AddressBookController(_contentLoaderMock.Object, _addressBookServiceMock.Object, _localizationService, _controllerExceptionHandler.Object);
        }
 private void InitializeTestData()
 {
     _addressBookService = new Mock <IAddressBookService>();
     classUnderTest      = new AddressBookController(_addressBookService.Object);
 }
        public async Task AddAddressAsync_ShouldBeOkObjectResult()
        {
            // Arrange
            var user = new User
            {
                Id = Guid.NewGuid()
            };

            var address = new Address(
                UserId.FromGuid(user.Id),
                Country.Canada,
                "Line1",
                null,
                "City",
                "State",
                "PostalCode");

            TestMock.UserService.Setup(userManager => userManager.GetUserAsync(It.IsAny <ClaimsPrincipal>())).ReturnsAsync(user).Verifiable();

            TestMock.AddressService.Setup(
                addressService => addressService.AddAddressAsync(
                    It.IsAny <UserId>(),
                    It.IsAny <Country>(),
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <string>()))
            .ReturnsAsync(DomainValidationResult <Address> .Succeeded(address))
            .Verifiable();

            var controller = new AddressBookController(TestMock.UserService.Object, TestMock.AddressService.Object, TestMapper);

            var request = new CreateAddressRequest
            {
                CountryIsoCode = EnumCountryIsoCode.CA,
                Line1          = "1234 Test Street",
                Line2          = null,
                City           = "Toronto",
                State          = "Ontario",
                PostalCode     = "A1A1A1"
            };

            // Act
            var result = await controller.AddAddressAsync(request);

            // Assert
            result.Should().BeOfType <OkObjectResult>();

            TestMock.UserService.Verify(userManager => userManager.GetUserAsync(It.IsAny <ClaimsPrincipal>()), Times.Once);

            TestMock.AddressService.Verify(
                addressService => addressService.AddAddressAsync(
                    It.IsAny <UserId>(),
                    It.IsAny <Country>(),
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <string>()),
                Times.Once);
        }
 public void BeforeEachTest()
 {
     _mockContactService = new Mock <IContactService>();
     _controller         = new AddressBookController(_mockContactService.Object, new NullLogger <AddressBookController>());
 }
Beispiel #14
0
 public AddressBookControllerShould()
 {
     controller = new AddressBookController(new AddressesStaticDataFileLoader());
 }
 public AddressBookControllerTests()
 {
     _addressBookController = new AddressBookController();
 }