public async Task OnCall_ReturnViewWithModel()
        {
            // Arrange
            _mockRepo.Setup(repo => repo.FindAllAsync())
            .ReturnsAsync(SampleDataCreators.GetTestLocations());

            // Act
            var result = await _sut.Index();

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <List <LocationVM> >(viewResult.Model);

            Assert.Equal("Index", viewResult.ViewName);
            Assert.Equal(2, model.Count);
        }
Example #2
0
        public void LocationIndex_WithLocations_DisplaysLocations()
        {
            var mockCustomerRepo     = new Mock <ICustomerRepo>();
            var mockStoreRepo        = new Mock <IStoreRepo>();
            var mockProductRepo      = new Mock <IProductRepo>();
            var mockOrderRepo        = new Mock <IOrderRepo>();
            var mockShoppingCartRepo = new Mock <IShoppingCart>();

            Dictionary <int, int> products = new Dictionary <int, int>()
            {
                { 1, 10 },
                { 2, 20 },
            };

            mockStoreRepo.Setup(r => r.GetAllStores())
            .Returns(new List <Store> {
                new Store(1, "Store", products)
            });

            var controller = new LocationsController(new NullLogger <LocationsController>(), mockStoreRepo.Object, mockCustomerRepo.Object, mockOrderRepo.Object, mockProductRepo.Object, mockShoppingCartRepo.Object);

            IActionResult actionResult = controller.Index();

            var viewResult    = Assert.IsAssignableFrom <ViewResult>(actionResult);
            var locations     = Assert.IsAssignableFrom <IEnumerable <LocationViewModel> >(viewResult.Model);
            var locationsList = locations.ToList();

            Assert.Equal(1, locationsList.Count());
            Assert.Equal("Store", locationsList[0].Name);
            Assert.Equal(products, locationsList[0].Inventory);
            Assert.Null(viewResult.ViewName);
        }
        public void Index()
        {
            var controller = new LocationsController();

            var result = controller.Index();

            Assert.IsNotNull(result);
        }
Example #4
0
        public void Generate_State_Specific_Location_Count()
        {
            // Arrange
            // - create the mock repository
            Mock <ILocationRepository> mock = new Mock <ILocationRepository>();

            mock.Setup(m => m.Locations).Returns(new List <Location>
            {
                new Location {
                    Id = 1, Name = "L1", State = "MI"
                },
                new Location {
                    Id = 2, Name = "L2", State = "IL"
                },
                new Location {
                    Id = 3, Name = "L3", State = "MI"
                },
                new Location {
                    Id = 4, Name = "L4", State = "IN"
                },
                new Location {
                    Id = 5, Name = "L5", State = "MI"
                }
            });
            // Arrange - create the controller
            LocationsController target = new LocationsController(mock.Object);

            target.PageSize = 3;
            ViewResult indx     = (ViewResult)target.Index("MI");
            ViewResult indxIl   = (ViewResult)target.Index("IL");
            ViewResult indxIn   = (ViewResult)target.Index("IN");
            ViewResult indxNull = (ViewResult)target.Index(null);

            // Act - get the set of states
            int res1   = ((LocationListViewModel)indx.Model).PagingInfo.TotalItems;
            int res2   = ((LocationListViewModel)indxIl.Model).PagingInfo.TotalItems;
            int res3   = ((LocationListViewModel)indxIn.Model).PagingInfo.TotalItems;
            int resAll = ((LocationListViewModel)indxNull.Model).PagingInfo.TotalItems;

            //Assert
            Assert.AreEqual(res1, 3);
            Assert.AreEqual(res2, 1);
            Assert.AreEqual(res3, 1);
            Assert.AreEqual(resAll, 5);
        }
Example #5
0
        public void IndexIsInitiallyEmpty()
        {
            var locationsController = new LocationsController(null, Context, null);
            var result = locationsController.Index();

            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <IEnumerable <LocationModel> >(viewResult.ViewData.Model);

            Assert.Empty(model);
        }
Example #6
0
        public async Task The_Location_Data_Is_Returned_Correctly()
        {
            // Arrange

            // Act
            var results = await _sut.Index("Search", true);

            // Assert
            _mockLocationsApiClient.Verify(m => m.SearchLocations("Search"), Times.Once);
            Assert.AreEqual(2, results.Count);
        }
Example #7
0
        public void Mock_GetViewResultIndex_Test() //Confirms route returns view
        {
            //Arrange
            DbSetup();
            LocationsController controller = new LocationsController(mock.Object);

            //Act
            var result = controller.Index();

            //Assert
            Assert.IsType <ViewResult>(result);
        }
        public void Mock_ViewResult_Index_Test()
        {
            //Arrange
            DbSetup();
            LocationsController controller = new LocationsController(mock.Object);

            //Act
            var result = controller.Index();

            //Assert
            Assert.IsType <ViewResult>(result);
        }
Example #9
0
        public async Task Index_NoParams_ResultModelIsEnumerableOfLocationViewModel()
        {
            // Arrange
            ILocationService    service    = Substitute.For <ILocationService>();
            LocationsController controller = GetController(locationService: service);

            // Act
            IActionResult result = await controller.Index();

            // Assert
            ViewResult viewResult = Assert.IsType <ViewResult>(result);

            Assert.IsAssignableFrom <IEnumerable <LocationViewModel> >(viewResult.ViewData.Model);
        }
Example #10
0
        public async Task Index_NoParams_RepoGetHitWithNoParams()
        {
            // Arrange
            ILocationService    service    = Substitute.For <ILocationService>();
            LocationsController controller = GetController(locationService: service);

            // Act
            await controller.Index();

            // Assert
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            service.Received(1).Get();
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
        }
Example #11
0
        public async Task And_Exception_Then_Returns_Internal_Server_Error(
            [Frozen] Mock <IMediator> mockMediator,
            [Greedy] LocationsController controller)
        {
            mockMediator
            .Setup(mediator => mediator.Send(
                       It.IsAny <GetAddressesQuery>(),
                       It.IsAny <CancellationToken>()))
            .Throws <InvalidOperationException>();

            var controllerResult = await controller.Index("AB1 2CD") as StatusCodeResult;

            controllerResult.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError);
        }
Example #12
0
        public void TestLoctionIndexLocalTypeView()
        {
            // Arrange
            var locations           = GetAllLocations();
            var mockLocationService = new Mock <ILocationService>();
            var mockPostCodeService = new Mock <IPostCodeService>();

            mockLocationService.Setup(r => r.GetAll(LocationType.Local)).Returns(locations);
            var controller = new LocationsController(mockLocationService.Object, mockPostCodeService.Object);

            // Act
            var result = controller.Index((int)LocationType.Local, null, 1);

            // Assert
            Assert.AreEqual(locations.Count(), ((LocationsIndexViewModel)((ViewResult)result).Model).Locations.Count);
        }
        public async Task And_Exception_Then_Returns_Bad_Request(
            string authorityName,
            string locationName,
            [Frozen] Mock <IMediator> mockMediator,
            [Greedy] LocationsController controller)
        {
            mockMediator
            .Setup(mediator => mediator.Send(
                       It.IsAny <GetLocationQuery>(),
                       It.IsAny <CancellationToken>()))
            .Throws <InvalidOperationException>();

            var controllerResult = await controller.Index(locationName, authorityName) as StatusCodeResult;

            controllerResult.StatusCode.Should().Be((int)HttpStatusCode.BadRequest);
        }
        public void DB_CreateNewEntry_Test()
        {
            // Arrange
            LocationsController controller   = new LocationsController(db);
            Location            testLocation = new Location();

            testLocation.Description = "TestDb Location";
            testLocation.Name        = "TestDb Name";

            // Act
            controller.Create(testLocation);
            var collection = (controller.Index() as ViewResult).ViewData.Model as IEnumerable <Location>;

            // Assert
            Assert.Contains <Location>(testLocation, collection);
        }
Example #15
0
        public void Mock_ConfirmEntry_Test() //Confirms presence of known entry
        {
            // Arrange
            DbSetup();
            LocationsController controller   = new LocationsController(mock.Object);
            Location            testLocation = new Location();

            testLocation.Name       = "Kyiv";
            testLocation.LocationId = 1;

            // Act
            ViewResult indexView  = controller.Index() as ViewResult;
            var        collection = indexView.ViewData.Model as IEnumerable <Location>;

            // Assert
            Assert.Contains <Location>(testLocation, collection);
        }
Example #16
0
        public void LocationsController_Index_Get_Default_Should_Pass()
        {
            // Arrange
            var dataMock    = new LocationsDataMock();
            var backendMock = new Mock <LocationsBackend>(context);

            backendMock.Setup(backend => backend.IndexAsync(false)).Returns(Task.FromResult(dataMock.GetAllViewModelList()));
            var myController = new LocationsController(context, backendMock.Object);


            // Act
            var result = myController.Index();


            // Assert
            Assert.IsNotNull(result);
        }
Example #17
0
        public void Index_ReturnsAView_WithAListOfLocations()
        {
            // Arrange
            var mockRepo = new Mock <ILocationRepository>();

            mockRepo.Setup(repo => repo.GetAll())
            .Returns(ListOfTwoLocations());
            var controller = new LocationsController(mockRepo.Object, null, null);

            // Act
            var result = controller.Index();

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <IEnumerable <Location> >(viewResult.ViewData.Model);

            Assert.Equal(2, model.Count());
        }
        public async Task And_Returns_Null_Returns_Not_Found(
            string authorityName,
            string locationName,
            [Frozen] Mock <IMediator> mockMediator,
            [Greedy] LocationsController controller)
        {
            mockMediator
            .Setup(mediator => mediator.Send(
                       It.IsAny <GetLocationQuery>(),
                       It.IsAny <CancellationToken>())).ReturnsAsync(new GetLocationQueryResult
            {
                Location = null
            });

            var controllerResult = await controller.Index(locationName, authorityName) as StatusCodeResult;

            controllerResult.StatusCode.Should().Be((int)HttpStatusCode.NotFound);
        }
Example #19
0
        public async Task Then_Gets_Addresses_From_Mediator(
            GetAddressesQueryResult mediatorResult,
            [Frozen] Mock <IMediator> mockMediator,
            [Greedy] LocationsController controller)
        {
            mockMediator
            .Setup(mediator => mediator.Send(
                       It.IsAny <GetAddressesQuery>(),
                       It.IsAny <CancellationToken>()))
            .ReturnsAsync(mediatorResult);

            var controllerResult = await controller.Index("AB1 2CD") as ObjectResult;

            Assert.IsNotNull(controllerResult);
            controllerResult.StatusCode.Should().Be((int)HttpStatusCode.OK);
            var model = controllerResult.Value as GetAddressesListResponse;

            Assert.IsNotNull(model);
            model.Addresses.Should().BeEquivalentTo(mediatorResult.AddressesResponse.Addresses.Select(item => item));
        }
Example #20
0
        public void Can_Send_Pagination_View_Model()
        {
            // Arrange
            Mock <ILocationRepository> mock = new Mock <ILocationRepository>();

            mock.Setup(m => m.Locations).Returns(new List <Location>
            {
                new Location {
                    Id = 1, Name = "L1"
                },
                new Location {
                    Id = 2, Name = "L2"
                },
                new Location {
                    Id = 3, Name = "L3"
                },
                new Location {
                    Id = 4, Name = "L4"
                },
                new Location {
                    Id = 5, Name = "L5"
                }
            });

            // Arrange
            LocationsController controller = new LocationsController(mock.Object);

            controller.PageSize = 3;
            ViewResult indx = (ViewResult)controller.Index(null, 2);
            //Act
            LocationListViewModel result =
                (LocationListViewModel)indx.Model;

            //Assert
            PagingInfo pageInfo = result.PagingInfo;

            Assert.AreEqual(pageInfo.CurrentPage, 2);
            Assert.AreEqual(pageInfo.ItemsPerPage, 3);
            Assert.AreEqual(pageInfo.TotalItems, 5);
            Assert.AreEqual(pageInfo.TotalPages, 2);
        }
Example #21
0
        public void Can_Filter_Locations()
        {
            // Arrange
            // - create the mock repository
            Mock <ILocationRepository> mock = new Mock <ILocationRepository>();

            mock.Setup(m => m.Locations).Returns(new List <Location>
            {
                new Location {
                    Id = 1, Name = "L1", State = "MI"
                },
                new Location {
                    Id = 2, Name = "L2", State = "IL"
                },
                new Location {
                    Id = 3, Name = "L3", State = "MI"
                },
                new Location {
                    Id = 4, Name = "L4", State = "IN"
                },
                new Location {
                    Id = 5, Name = "L5", State = "MI"
                }
            });

            // Arrange - create a controller and make the page size 3 items
            LocationsController controller = new LocationsController(mock.Object);

            controller.PageSize = 3;

            ViewResult indx = (ViewResult)controller.Index("MI", 1);

            // Action
            Location[] result = ((LocationListViewModel)indx.Model)
                                .Locations.ToArray();
            //Assert
            Assert.AreEqual(result.Length, 3);
            Assert.IsTrue(result[0].Name == "L1" && result[0].State == "MI");
            Assert.IsTrue(result[2].Name == "L5" && result[2].State == "MI");
        }
        public async Task Then_Gets_Location_From_Mediator(
            string authorityName,
            string locationName,
            GetLocationQueryResult queryResult,
            [Frozen] Mock <IMediator> mockMediator,
            [Greedy] LocationsController controller)
        {
            mockMediator
            .Setup(mediator => mediator.Send(
                       It.Is <GetLocationQuery>(request =>
                                                request.LocationName == locationName &&
                                                request.AuthorityName == authorityName),
                       It.IsAny <CancellationToken>()))
            .ReturnsAsync(queryResult);

            var controllerResult = await controller.Index(locationName, authorityName) as ObjectResult;

            var model = controllerResult.Value as GetLocationsListItem;

            controllerResult.StatusCode.Should().Be((int)HttpStatusCode.OK);
            model.Should().BeEquivalentTo(queryResult.Location, options => options.ExcludingMissingMembers());
        }
Example #23
0
        public void Can_Locations_Paginate()
        {
            // Arrange
            Mock <ILocationRepository> mock = new Mock <ILocationRepository>();

            mock.Setup(m => m.Locations).Returns(new List <Location>
            {
                new Location {
                    Id = 1, Name = "L1"
                },
                new Location {
                    Id = 2, Name = "L2"
                },
                new Location {
                    Id = 3, Name = "L3"
                },
                new Location {
                    Id = 4, Name = "L4"
                },
                new Location {
                    Id = 5, Name = "L5"
                }
            });

            LocationsController controller = new LocationsController(mock.Object);

            controller.PageSize = 3;
            ViewResult indx = (ViewResult)controller.Index(null, 2);
            //Act
            LocationListViewModel result =
                (LocationListViewModel)indx.Model;

            //Assert
            Location[] prodArray = result.Locations.ToArray();
            Assert.IsTrue(prodArray.Length == 2);
            Assert.AreEqual(prodArray[0].Name, "L4");
            Assert.AreEqual(prodArray[1].Name, "L5");
        }
Example #24
0
        public void Contains_All_Locations()
        {
            // Arrange - create the mock repository
            Mock <ILocationRepository> mock = new Mock <ILocationRepository>();

            mock.Setup(m => m.Locations).Returns(new List <Location>
            {
                new Location {
                    Id = 1, Name = "L1", State = "MI"
                },
                new Location {
                    Id = 2, Name = "L2", State = "IL"
                },
                new Location {
                    Id = 3, Name = "L3", State = "MI"
                },
                new Location {
                    Id = 4, Name = "L4", State = "IN"
                },
                new Location {
                    Id = 5, Name = "L5", State = "MI"
                }
            });

            // Arrange
            LocationsController target = new LocationsController(mock.Object);
            ViewResult          indx   = (ViewResult)target.Index(null);

            //Action
            Location[] result = ((LocationListViewModel)indx.Model).Locations.ToArray();

            //Assert
            Assert.AreEqual(result.Length, 5);
            Assert.AreEqual("L1", result[0].Name);
            Assert.AreEqual("L2", result[1].Name);
            Assert.AreEqual("L3", result[2].Name);
        }