コード例 #1
0
        public async Task GetAllAsync_GetDestinationsPreviewQuery_Can_Verify()
        {
            // Arrange
            var mockMediator = new Mock <IMediator>();

            mockMediator.Setup(x => x.Send(It.IsAny <GetDestinationsPreviewQuery>(), It.IsAny <CancellationToken>())).ReturnsAsync(Destinations);
            var mockMapper          = new Mock <IMapper>();
            var mockResponseFactory = new Mock <IResponseFactory>();
            var mockApiSettings     = new Mock <IOptionsSnapshot <ApiSettings> >();

            mockApiSettings.SetupGet(x => x.Value).Returns(ApiSettings);
            var sut = new DestinationsController(mockMediator.Object, mockMapper.Object, mockResponseFactory.Object, mockApiSettings.Object);

            var pagingOptions = new PagingOptions
            {
                PageNumber = 1,
                PageSize   = 10
            };

            var sortOptions = new SortOptions
            {
                OrderBy = "Name"
            };

            var searchOptions = new SearchOptions
            {
                Query = "London"
            };

            // Act
            await sut.GetAllDestinationsAsync(pagingOptions, sortOptions, searchOptions, default(CancellationToken));

            // Assert
            mockMediator.Verify(x => x.Send(It.IsAny <GetDestinationsPreviewQuery>(), It.IsAny <CancellationToken>()));
        }
コード例 #2
0
        public async Task TestParameterisedGetMethodReturnsADestinationObjectsAsync()
        {
            var destination = new Destination
            {
                DestinationId = 1,
                Country       = "England",
                City          = "London"
            };

            var expectedResult = new Destination
            {
                DestinationId = 1,
                Country       = "England",
                City          = "London"
            };



            var mock = new Mock <IDataProvider>();

            mock.Setup(x => x.GetDestination(1)).ReturnsAsync(destination);

            var destinationsController = new DestinationsController(mock.Object);

            var destinationObject = await destinationsController.Get(1);

            destinationObject.Should().BeEquivalentTo(expectedResult, "this is the list of destinations from the database");
        }
コード例 #3
0
        public void TestInitialize()
        {
            var options = new DbContextOptionsBuilder <f19Context>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            db = new f19Context(options);

            var region = new Region
            {
                RegionId = 871,
                Name     = "Oceania"
            };

            destinations.Add(new Destination
            {
                DestinationId = 534,
                Name          = "One Cool Place",
                Location      = "South Oz",
                Region        = region
            });

            destinations.Add(new Destination
            {
                DestinationId = 535,
                Name          = "Second Cool Place",
                Location      = "West Oz",
                Region        = region
            });

            destinations.Add(new Destination
            {
                DestinationId = 536,
                Name          = "Third Cool Place",
                Location      = "North Oz",
                Region        = region
            });

            destinations.Add(new Destination
            {
                DestinationId = 537,
                Name          = "Fourth Cool Place",
                Location      = "East Oz",
                Region        = region
            });

            foreach (var d in destinations)
            {
                db.Add(d);
            }

            db.SaveChanges();
            destinationsController = new DestinationsController(db);
        }
コード例 #4
0
        public void CreateDestinationsController_WhenParamsAreValid()
        {
            // Arrange
            var mockedIDestinationService = new Mock <IDestinationService>();
            var mockedMappingService      = new Mock <IMappingService>();
            var mockedImageService        = new Mock <IImageService>();
            var destinationsController    = new DestinationsController(mockedIDestinationService.Object, mockedMappingService.Object, mockedImageService.Object);

            //Act & Assert
            Assert.That(destinationsController, Is.InstanceOf <DestinationsController>());
        }
コード例 #5
0
        public void ReturnDefaultView()
        {
            // Arrange
            var mockedIDestinationService = new Mock <IDestinationService>();
            var mockedMappingService      = new Mock <IMappingService>();
            var mockedImageService        = new Mock <IImageService>();
            var destinationsController    = new DestinationsController(mockedIDestinationService.Object, mockedMappingService.Object, mockedImageService.Object);

            //Act & Assert
            destinationsController
            .WithCallTo(c => c.Index())
            .ShouldRenderDefaultView();
        }
コード例 #6
0
        public async Task GetAllAsync_Returns_OK_200_Result_With_Correct_Data()
        {
            // Arrange
            var mockMediator = new Mock <IMediator>();

            mockMediator.Setup(x => x.Send(It.IsAny <GetDestinationsPreviewQuery>(), It.IsAny <CancellationToken>())).ReturnsAsync(Destinations);
            var mapper = Mapper;
            var mockResponseFactory = new Mock <IResponseFactory>();

            mockResponseFactory.Setup(x => x.CreatePagedReponse(It.IsAny <IList <Destination> >(), It.IsAny <Type>(), It.IsAny <IPreviewQuery>(), It.IsAny <ResponseStatus>(), It.IsAny <string>())).Returns(PagedDestinations);
            var mockApiSettings = new Mock <IOptionsSnapshot <ApiSettings> >();

            mockApiSettings.SetupGet(x => x.Value).Returns(ApiSettings);
            var sut = new DestinationsController(mockMediator.Object, mapper, mockResponseFactory.Object, mockApiSettings.Object);

            var pagingOptions = new PagingOptions
            {
                PageNumber = 1,
                PageSize   = 10
            };

            var sortOptions = new SortOptions
            {
                OrderBy = "Name"
            };

            var searchOptions = new SearchOptions
            {
                Query = "London"
            };

            // Act
            var result = await sut.GetAllDestinationsAsync(pagingOptions, sortOptions, searchOptions, default(CancellationToken));

            // Assert
            var response = result.Should().BeOfType <OkObjectResult>().Subject;

            response.StatusCode.Should().Equals(200);

            var destination = response.Value.Should().BeAssignableTo <PagedResponse <Destination> >().Subject;

            destination.Data.Select(x => x.Id == 1).Should().NotBeNull();
            destination.Data.Select(x => x.Name == "London").Should().NotBeNull();
            destination.Data.Count().Should().Equals(3);
        }
コード例 #7
0
        public void ReturnViewWithModel()
        {
            // Arrange
            var mockedIDestinationService = new Mock <IDestinationService>();
            var mockedMappingService      = new Mock <IMappingService>();
            var mockedImageService        = new Mock <IImageService>();
            var destinationsController    = new DestinationsController(mockedIDestinationService.Object, mockedMappingService.Object, mockedImageService.Object);

            destinationsController.ModelState.AddModelError("", "dummy error");

            DestinationViewModel model = new DestinationViewModel();

            // Act & Assert
            destinationsController
            .WithCallTo(c => c.Create(model))
            .ShouldRenderDefaultView()
            .WithModel(model);
        }
コード例 #8
0
        public async Task GetByIdAsync_GetDestinationPreviewQuery_Has_Correct_Data()
        {
            // Arrange
            var mockMediator        = new Mock <IMediator>();
            var mockMapper          = new Mock <IMapper>();
            var mockResponseFactory = new Mock <IResponseFactory>();
            var mockApiSettings     = new Mock <IOptionsSnapshot <ApiSettings> >();

            mockApiSettings.SetupGet(x => x.Value).Returns(ApiSettings);
            var sut           = new DestinationsController(mockMediator.Object, mockMapper.Object, mockResponseFactory.Object, mockApiSettings.Object);
            var destinationId = 1;

            // Act
            await sut.GetDestinationByIdAsync(destinationId, default(CancellationToken));

            // Assert
            mockMediator.Verify(x => x.Send(It.Is <GetDestinationPreviewQuery>(d => d.Id == destinationId), It.IsAny <CancellationToken>()));
        }
コード例 #9
0
        public void GetAllAsync_Has_Route_Attribute()
        {
            // Arrange
            var mockMediator        = new Mock <IMediator>();
            var mockMapper          = new Mock <IMapper>();
            var mockResponseFactory = new Mock <IResponseFactory>();
            var mockApiSettings     = new Mock <IOptionsSnapshot <ApiSettings> >();

            mockApiSettings.SetupGet(x => x.Value).Returns(ApiSettings);
            var sut = new DestinationsController(mockMediator.Object, mockMapper.Object, mockResponseFactory.Object, mockApiSettings.Object);

            // Act
            var attributes = sut.GetAttributesOn(x => x.GetAllDestinationsAsync(
                                                     It.IsAny <PagingOptions>(),
                                                     It.IsAny <SortOptions>(),
                                                     It.IsAny <SearchOptions>(),
                                                     It.IsAny <CancellationToken>())).OfType <RouteAttribute>().SingleOrDefault();

            // Assert
            attributes.Should().NotBeNull();
            attributes.Name.Should().Be("GetAllDestinationsAsync");
        }
コード例 #10
0
        public async Task GetByIdAsync_Returns_OK_200_Result()
        {
            // Arrange
            var mockMediator = new Mock <IMediator>();

            mockMediator.Setup(x => x.Send(It.IsAny <GetDestinationPreviewQuery>(), It.IsAny <CancellationToken>())).ReturnsAsync(Destination);
            var mockMapper          = new Mock <IMapper>();
            var mockResponseFactory = new Mock <IResponseFactory>();
            var mockApiSettings     = new Mock <IOptionsSnapshot <ApiSettings> >();

            mockApiSettings.SetupGet(x => x.Value).Returns(ApiSettings);
            var sut           = new DestinationsController(mockMediator.Object, mockMapper.Object, mockResponseFactory.Object, mockApiSettings.Object);
            var destinationId = 1;

            // Act
            var result = await sut.GetDestinationByIdAsync(destinationId, default(CancellationToken));

            // Assert
            var response = result.Should().BeOfType <OkObjectResult>().Subject;

            response.StatusCode.Should().Equals(200);
        }
コード例 #11
0
        public async Task GetAllAsync_Returns_OK_200_Result()
        {
            // Arrange
            var mockMediator = new Mock <IMediator>();

            mockMediator.Setup(x => x.Send(It.IsAny <GetDestinationsPreviewQuery>(), It.IsAny <CancellationToken>())).ReturnsAsync(Destinations);
            var mapper = Mapper;
            var mockResponseFactory = new Mock <IResponseFactory>();
            var mockApiSettings     = new Mock <IOptionsSnapshot <ApiSettings> >();

            mockApiSettings.SetupGet(x => x.Value).Returns(ApiSettings);
            var sut = new DestinationsController(mockMediator.Object, mapper, mockResponseFactory.Object, mockApiSettings.Object);

            var pagingOptions = new PagingOptions
            {
                PageNumber = 1,
                PageSize   = 10
            };

            var sortOptions = new SortOptions
            {
                OrderBy = "Name"
            };

            var searchOptions = new SearchOptions
            {
                Query = "London"
            };

            // Act
            var result = await sut.GetAllDestinationsAsync(pagingOptions, sortOptions, searchOptions, default(CancellationToken));

            // Assert
            var response = result.Should().BeOfType <OkObjectResult>().Subject;

            response.StatusCode.Should().Equals(200);
        }
コード例 #12
0
        public void GetAllAsync_Has_ProducesResponse_Atttribute_With_Correct_Return_Type()
        {
            // Arrange
            var mockMediator        = new Mock <IMediator>();
            var mockMapper          = new Mock <IMapper>();
            var mockResponseFactory = new Mock <IResponseFactory>();
            var mockApiSettings     = new Mock <IOptionsSnapshot <ApiSettings> >();

            mockApiSettings.SetupGet(x => x.Value).Returns(ApiSettings);
            var sut           = new DestinationsController(mockMediator.Object, mockMapper.Object, mockResponseFactory.Object, mockApiSettings.Object);
            var expectedTyped = typeof(IEnumerable <DestinationPreviewDto>);

            // Act
            var attributes = sut.GetAttributesOn(x => x.GetAllDestinationsAsync(
                                                     It.IsAny <PagingOptions>(),
                                                     It.IsAny <SortOptions>(),
                                                     It.IsAny <SearchOptions>(),
                                                     It.IsAny <CancellationToken>())).OfType <ProducesResponseTypeAttribute>();

            // Assert
            attributes.Should().NotBeNull();
            attributes.Count().Should().BeGreaterThan(0);
            attributes.Select(x => x.Type == expectedTyped).Should().NotBeNull();
        }
コード例 #13
0
        public async Task TestGetMethodReturnsCollectionOfDestinationObjectsAsync()
        {
            var destinations = new List <Destination>
            {
                new Destination
                {
                    DestinationId = 1,
                    Country       = "England",
                    City          = "London"
                },

                new Destination
                {
                    DestinationId = 2,
                    Country       = "England",
                    City          = "Manchester"
                },

                new Destination
                {
                    DestinationId = 3,
                    Country       = "Spain",
                    City          = "Madrid"
                },

                new Destination
                {
                    DestinationId = 4,
                    Country       = "France",
                    City          = "Paris"
                }
            };

            var expectedResult = new List <Destination>
            {
                new Destination
                {
                    DestinationId = 1,
                    Country       = "England",
                    City          = "London"
                },

                new Destination
                {
                    DestinationId = 2,
                    Country       = "England",
                    City          = "Manchester"
                },

                new Destination
                {
                    DestinationId = 3,
                    Country       = "Spain",
                    City          = "Madrid"
                },

                new Destination
                {
                    DestinationId = 4,
                    Country       = "France",
                    City          = "Paris"
                }
            };

            var mock = new Mock <IDataProvider>();

            mock.Setup(x => x.GetDestinations()).ReturnsAsync(destinations.AsEnumerable());

            var destinationsController = new DestinationsController(mock.Object);

            var collectionOfDestinations = await destinationsController.Get();

            collectionOfDestinations.Should().BeEquivalentTo(expectedResult.AsEnumerable(), "this is the list of destinations from the database");
        }