Beispiel #1
0
        public async Task ExceptionTest()
        {
            // Arrange
            IManufacturerRepository mockRepo = Substitute.For <IManufacturerRepository>();

            mockRepo
            .GetAllManufacturersAsync()
            .ThrowsForAnyArgs(new Exception("Test Exception"));

            IDistributedCache   mockCache   = Substitute.For <IDistributedCache>();
            IOptions <Settings> appSettings = Substitute.For <IOptions <Settings> >();

            var mockLocalizer = new MockStringLocalizer <ManufacturersController>();

            IManufacturersApi theApi = new ManufacturersApi(appSettings, mockRepo);
            var controller           = new ManufacturersController(mockCache, theApi, mockLocalizer);

            //// Act
            var actionResult = await controller.Get();

            var objectResult = actionResult as Microsoft.AspNetCore.Mvc.ObjectResult;

            ////// Assert
            Assert.NotNull(objectResult);
            Assert.Equal(objectResult.StatusCode, (int)System.Net.HttpStatusCode.InternalServerError);
        }
Beispiel #2
0
        public async Task SuccessTest()
        {
            // Arrange
            IManufacturerRepository mockRepo = Substitute.For <IManufacturerRepository>();

            var repositoryReturnValue = new List <ManufacturerModel>()
            {
                new ManufacturerModel()
                {
                    Description = "Description 1",
                    MfgCode     = 1
                },
                new ManufacturerModel()
                {
                    Description = "Description 2",
                    MfgCode     = 2
                }
            };

            mockRepo
            .GetAllManufacturersAsync()
            .ReturnsForAnyArgs(Task.FromResult <IEnumerable <ManufacturerModel> >(repositoryReturnValue));

            IDistributedCache   mockCache   = Substitute.For <IDistributedCache>();
            IOptions <Settings> appSettings = Substitute.For <IOptions <Settings> >();

            var mockLocalizer = new MockStringLocalizer <ManufacturersController>();

            IManufacturersApi theApi = new ManufacturersApi(appSettings, mockRepo);
            var controller           = new ManufacturersController(mockCache, theApi, mockLocalizer);

            //// Act
            var response = await controller.Get();

            ////// Assert
            var actualRecord = ((Microsoft.AspNetCore.Mvc.ObjectResult)response).Value;

            Assert.Equal(((List <ManufacturerModel>)actualRecord).Count, repositoryReturnValue.Count);
        }