//Would be revisiting to modify the actual way of call method. public void MerchantListRepositoryTest_Success() { // Arrange int CustomerID = 191809; string mid = "191807"; MockMerchantListRepository mockMerchantListRepository = new MockMerchantListRepository(); ApiResult <GenericPaginationResponse <Merchant> > expectedResult = mockMerchantListRepository.GetMockData(CustomerID); PaginationMerchant page = mockMerchantListRepository.GetPagination(); IOptions <DataContext> optionsAccessor = Substitute.For <IOptions <DataContext> >(); IDatabaseConnectionFactory connectionFactory = Substitute.For <IDatabaseConnectionFactory>(); IMerchantListRepository mockRepo = Substitute.For <IMerchantListRepository>(); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); mockRepo.GetMerchantListAsync(CustomerID, page).ReturnsForAnyArgs(expectedResult.Result); // Act var merchList = mockRepo.GetMerchantListAsync(CustomerID, page).Result; var actualRecord = (IList <Wp.CIS.LynkSystems.Model.Merchant>)merchList.ReturnedRecords; string merchInfo = actualRecord.Where(x => x.MID == mid).FirstOrDefault().Name; //// Assert Assert.Equal(((IList <Merchant>)actualRecord).Count, 2); Assert.Equal(merchInfo, "ABC Corp"); }
public async Task MerchantListApiTest_Exception() { // Arrange int CustomerID = 191809; MockMerchantListRepository mockMerchantListRepository = new MockMerchantListRepository(); ApiResult <GenericPaginationResponse <Merchant> > expectedResult = mockMerchantListRepository.GetMockData(CustomerID); PaginationMerchant page = mockMerchantListRepository.GetPagination(); IOptions <Settings> optionsAccessor = Substitute.For <IOptions <Settings> >(); IMerchantListRepository mockRepo = Substitute.For <IMerchantListRepository>(); IMerchantListApi merchantListApi = Substitute.For <IMerchantListApi>(); IDistributedCache mockCache = Substitute.For <IDistributedCache>(); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); mockRepo.GetMerchantListAsync(CustomerID, page).Throws(new Exception()); merchantListApi = new MerchantListApi(optionsAccessor, mockRepo, loggingFacade); //Assert await Assert.ThrowsAsync <Exception>(() => merchantListApi.GetMerchantListAsync(CustomerID, page)); }
public async Task MerchantListControllerTest_NoDataFound() { // Arrange int CustomerID = 191809; IConfigurationRoot configurationRoot = Substitute.For <IConfigurationRoot>(); configurationRoot = GetConfiguration(configurationRoot); MockMerchantListRepository mockMerchantListRepository = new MockMerchantListRepository(); ApiResult <GenericPaginationResponse <Merchant> > expectedResult = mockMerchantListRepository.GetMockData(CustomerID); PaginationMerchant page = mockMerchantListRepository.GetPagination(); MerchantListInput pageinput = new MerchantListInput(); pageinput.LIDValue = CustomerID.ToString(); pageinput.lidTypeEnum = Wp.CIS.LynkSystems.Model.Enums.LidTypeEnum.Customer; pageinput.Page = page; IDistributedCache mockCache = Substitute.For <IDistributedCache>(); IStringLocalizer <MerchantListController> localizer = Substitute.For <IStringLocalizer <MerchantListController> >(); string key = "NoDataFound"; string value = "No data found for provided ID"; var localizedString = new LocalizedString(key, value); localizer[Arg.Any <string>()].ReturnsForAnyArgs(localizedString); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); IMerchantListApi merchantListApi = Substitute.For <IMerchantListApi>(); IOperation fakeOperation = Substitute.For <Operation>(mockCache); fakeOperation.WhenForAnyArgs(x => x.RetrieveCache(Arg.Any <string>(), Arg.Any <ICollection <Merchant> >())).DoNotCallBase(); ApiResult <GenericPaginationResponse <Merchant> > response = new ApiResult <GenericPaginationResponse <Merchant> >(); response.Result = new GenericPaginationResponse <Merchant>(); merchantListApi.GetMerchantListAsync(CustomerID, page).ReturnsForAnyArgs(response); MerchantListController controller = new MerchantListController(mockCache, merchantListApi, localizer, fakeOperation, loggingFacade); // Act var merchList = await controller.GetMerchantList(pageinput); // Assert Assert.Equal(((Microsoft.AspNetCore.Mvc.ObjectResult)merchList).StatusCode, 200); var actualMerchantList = ((Microsoft.AspNetCore.Mvc.ObjectResult)merchList).Value; Assert.Equal(((GenericPaginationResponse <Merchant>)actualMerchantList).ModelMessage, localizer["NoDataFound"].Value); }
//Mock API Call and unit test for the API call with returning mock MerchantList. public async Task MerchantListControllerTest_Success() { // Arrange int CustomerID = 191809; string mid = "191807"; IConfigurationRoot configurationRoot = Substitute.For <IConfigurationRoot>(); configurationRoot = GetConfiguration(configurationRoot); MockMerchantListRepository mockMerchantListRepository = new MockMerchantListRepository(); ApiResult <GenericPaginationResponse <Merchant> > expectedResult = mockMerchantListRepository.GetMockData(CustomerID); PaginationMerchant page = mockMerchantListRepository.GetPagination(); MerchantListInput pageinput = new MerchantListInput(); pageinput.LIDValue = CustomerID.ToString(); pageinput.lidTypeEnum = Wp.CIS.LynkSystems.Model.Enums.LidTypeEnum.Customer; pageinput.Page = page; IDistributedCache mockCache = Substitute.For <IDistributedCache>(); IMerchantListRepository mockRepo = Substitute.For <IMerchantListRepository>(); IStringLocalizer <MerchantListController> localizer = Substitute.For <IStringLocalizer <MerchantListController> >(); IMerchantListApi mockMerchantListApi = Substitute.For <IMerchantListApi>(); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); IOperation fakeOperation = Substitute.For <Operation>(mockCache); fakeOperation.WhenForAnyArgs(x => x.RetrieveCache(Arg.Any <string>(), Arg.Any <GenericPaginationResponse <Merchant> >())).DoNotCallBase(); fakeOperation.WhenForAnyArgs(x => x.AddCacheAsync(Arg.Any <string>(), Arg.Any <GenericPaginationResponse <Merchant> >())).DoNotCallBase(); MerchantListController controller = new MerchantListController(mockCache, mockMerchantListApi, localizer, fakeOperation, loggingFacade); mockMerchantListApi.GetMerchantListAsync(CustomerID, page).ReturnsForAnyArgs(expectedResult); // Act var merchList = await controller.GetMerchantList(pageinput); var actualRecord = ((Microsoft.AspNetCore.Mvc.ObjectResult)merchList).Value; string merchInfo = ((IList <Merchant>)((GenericPaginationResponse <Merchant>)actualRecord).ReturnedRecords).Where(x => x.MID == mid).FirstOrDefault().Name; // Assert var recordCount = ((GenericPaginationResponse <Merchant>)actualRecord).ReturnedRecords; Assert.Equal(recordCount.ToList().Count, 2); Assert.Equal(merchInfo, "ABC Corp"); }
public void MerchantListControllerTest_ModelState_Invalid() { //Arrange int CustomerID = 191807; IConfigurationRoot configurationRoot = Substitute.For <IConfigurationRoot>(); configurationRoot = GetConfiguration(configurationRoot); MockMerchantListRepository mockMerchantListRepository = new MockMerchantListRepository(); ApiResult <GenericPaginationResponse <Merchant> > expectedResult = mockMerchantListRepository.GetMockData(CustomerID); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); PaginationMerchant page = mockMerchantListRepository.GetPagination(); MerchantListInput pageinput = new MerchantListInput(); pageinput.LIDValue = CustomerID.ToString(); pageinput.lidTypeEnum = Wp.CIS.LynkSystems.Model.Enums.LidTypeEnum.Customer; pageinput.Page = page; IStringLocalizer <MerchantListController> localizer = Substitute.For <IStringLocalizer <MerchantListController> >(); IMerchantListApi merchantListApi = Substitute.For <IMerchantListApi>(); IDistributedCache mockCache = FakeCache(); IOperation fakeOperation = Substitute.ForPartsOf <Operation>(mockCache); MerchantListController controller = new MerchantListController(mockCache, merchantListApi, localizer, fakeOperation, loggingFacade); //Act controller.ModelState.AddModelError("key", "error message"); var result = controller.GetMerchantList(pageinput); //Assert Assert.Equal(((Microsoft.AspNetCore.Mvc.ObjectResult)result.Result).StatusCode.ToString(), "400"); }