public async Task ReturnCorrectViewModel_WhenCalled() { // Arrange Mock <ILeagueService> leagueServiceMock = new Mock <ILeagueService>(); Mock <IPDFService> pDFServiceMock = new Mock <IPDFService>(); Mock <IMemoryCache> memoryCacheMock = new Mock <IMemoryCache>(); string validSortOrder = string.Empty; string validFilter = string.Empty; int validPageNumber = 1; int validPageSize = 10; IPagedList <League> leagues = new PagedList <League>(new List <League>().AsQueryable(), validPageNumber, validPageSize); leagueServiceMock.Setup(mock => mock.FilterLeaguesAsync(validSortOrder, validFilter, validPageNumber, validPageSize)) .Returns(Task.FromResult(leagues)); LeagueController SUT = new LeagueController( leagueServiceMock.Object, pDFServiceMock.Object, memoryCacheMock.Object); // Act var result = await SUT.Filter(validSortOrder, validFilter, validPageSize, validPageNumber) as PartialViewResult; // Assert Assert.IsInstanceOfType(result.Model, typeof(TableViewModel <LeagueViewModel>)); }
public async Task ReturnViewResult_WhenCalled() { // Arrange Mock <ILeagueService> leagueServiceMock = new Mock <ILeagueService>(); Mock <IPDFService> pDFServiceMock = new Mock <IPDFService>(); IMemoryCache memoryCacheMock = new MemoryCache(new MemoryCacheOptions()); string validSortOrder = string.Empty; string validFilter = string.Empty; int validPageNumber = 1; int validPageSize = 10; IPagedList <League> leagues = new PagedList <League>(new List <League>().AsQueryable(), validPageNumber, validPageSize); leagueServiceMock.Setup(mock => mock.FilterLeaguesAsync(validSortOrder, validFilter, validPageNumber, validPageSize)) .Returns(Task.FromResult(leagues)); LeagueController SUT = new LeagueController( leagueServiceMock.Object, pDFServiceMock.Object, memoryCacheMock); // Act var result = await SUT.Index(); // Assert Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void AddLeagueInTheRepo() { List <League> leagues = CreateLeagueList(); List <League> added = new List <League>(); var mock = new Mock <ILeagueRepository>(MockBehavior.Strict); // Filling mock with data mock.As <ICRUDRepository <League, int, LeagueFilter> >().Setup(m => m.Add(It.IsAny <League>())) .Returns(Task.FromResult(leagues.FirstOrDefault())) .Callback <League>(c => added.Add(c)); mock.As <ILeagueRepository>().Setup(m => m.isLeagueNameExist(It.IsAny <int>(), It.IsAny <string>(), null)) .Returns(Task.FromResult(false)); var mockSeasonRepo = new Mock <ISeasonRepository>(MockBehavior.Strict); mockSeasonRepo.As <ICRUDRepository <Season, int, SeasonFilter> >().Setup(m => m.Get(It.IsAny <int>())) .Returns <int>(id => Task.FromResult(new Season())); // Creating the controller which we want to create LeagueController controller = new LeagueController(mock.Object, mockSeasonRepo.Object); // configuring the context for the controler fakeContext(controller); // Testing all the list that we can retrieve correctly the leagues for (int i = 0; i < leagues.Count; i++) { HttpResponseMessage response = controller.Post(leagues[i]).Result; // the result should say "HttpStatusCode.Created" Assert.AreEqual(response.StatusCode, HttpStatusCode.Created); } // the added list should be the same as the list CollectionAssert.AreEqual(leagues, added); }
public void UpdateFailureLeagueInTheRepo() { League league = new League(); var mock = new Mock <ILeagueRepository>(MockBehavior.Strict); // Creating the rules for mock, always send true in this case mock.As <ICRUDRepository <League, int, LeagueFilter> >().Setup(m => m.Update(It.IsAny <int>(), It.IsAny <League>())) .Returns(Task.FromResult(true)); mock.As <ILeagueRepository>().Setup(m => m.isLeagueNameExist(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <int>())) .Returns(Task.FromResult(false)); var mockSeasonRepo = new Mock <ISeasonRepository>(MockBehavior.Strict); mockSeasonRepo.As <ICRUDRepository <Season, int, SeasonFilter> >().Setup(m => m.Get(It.IsAny <int>())) .Returns <int>(id => Task.FromResult(new Season())); // Creating the controller which we want to create LeagueController controller = new LeagueController(mock.Object, mockSeasonRepo.Object); // configuring the context for the controler fakeContext(controller); // Facking a model error controller.ModelState.AddModelError("key", "errorMessage"); HttpResponseMessage response = controller.Put(league.Id, league).Result; // the result should say "HttpStatusCode.BadRequest" Assert.AreEqual(response.StatusCode, HttpStatusCode.BadRequest); }
public async Task ThrowApplicationException_WhenLeaguesIsNull() { // Arrange Mock <ILeagueService> leagueServiceMock = new Mock <ILeagueService>(); Mock <IPDFService> pDFServiceMock = new Mock <IPDFService>(); Mock <IMemoryCache> memoryCacheMock = new Mock <IMemoryCache>(); string validSortOrder = string.Empty; string validFilter = string.Empty; int validPageNumber = 1; int validPageSize = 10; IPagedList <League> leagues = null; leagueServiceMock.Setup(mock => mock.FilterLeaguesAsync(validSortOrder, validFilter, validPageNumber, validPageSize)) .Returns(Task.FromResult(leagues)); LeagueController SUT = new LeagueController( leagueServiceMock.Object, pDFServiceMock.Object, memoryCacheMock.Object); // Act & Assert await Assert.ThrowsExceptionAsync <ApplicationException>(() => SUT.Download(validSortOrder, validFilter, validPageNumber, validPageSize)); }
public void UpdateFailureLeagueNameExistsInTheRepo() { League league = new League(); var mock = new Mock <ILeagueRepository>(MockBehavior.Strict); // Filling mock rull with repository mock.As <ICRUDRepository <League, int, LeagueFilter> >().Setup(m => m.Add(It.IsAny <League>())); mock.As <ILeagueRepository>().Setup(m => m.isLeagueNameExist(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <int>())) .Returns(Task.FromResult(true)); var mockSeasonRepo = new Mock <ISeasonRepository>(MockBehavior.Strict); mockSeasonRepo.As <ICRUDRepository <Season, int, SeasonFilter> >().Setup(m => m.Get(It.IsAny <int>())) .Returns <int>(id => Task.FromResult(new Season())); // Creating the controller which we want to create LeagueController controller = new LeagueController(mock.Object, mockSeasonRepo.Object); // configuring the context for the controler fakeContext(controller); HttpResponseMessage response = controller.Put(1, league).Result; // the result should say "HttpStatusCode.BadRequest" Assert.AreEqual(response.StatusCode, HttpStatusCode.BadRequest); }
public async void TestForGetLeagueById() { var options = new DbContextOptionsBuilder <LeagueContext>() .UseInMemoryDatabase(databaseName: "p3LeagueControllerGetLeagueById") .Options; using (var context = new LeagueContext(options)) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); Repo r = new Repo(context, new NullLogger <Repo>()); Logic logic = new Logic(r, new NullLogger <Repo>()); LeagueController leagueController = new LeagueController(logic); var league = new League { LeagueID = Guid.NewGuid(), LeagueName = "louge", SportID = 51 }; var getLeague = await leagueController.GetLeagueById(league.LeagueID); Assert.IsAssignableFrom <string>((getLeague as NotFoundObjectResult).Value); r.Leagues.Add(league); await r.CommitSave(); var getLeague2 = await leagueController.GetLeagueById(league.LeagueID); Assert.IsAssignableFrom <League>((getLeague2 as OkObjectResult).Value); } }
public void RetrieveFailureALeagueInTheRepo() { List <League> leagues = CreateLeagueList(); var mock = new Mock <ILeagueRepository>(MockBehavior.Strict); // Filling mock with data mock.As <ICRUDRepository <League, int, LeagueFilter> >().Setup(m => m.Get(It.IsAny <int>())) .Returns <int?>(id => Task.FromResult(leagues.FirstOrDefault(c => false))); var mockSeasonRepo = new Mock <ISeasonRepository>(MockBehavior.Strict); mockSeasonRepo.As <ICRUDRepository <Season, int, SeasonFilter> >().Setup(m => m.Get(It.IsAny <int>())) .Returns <int>(id => Task.FromResult(new Season())); // Creating the controller which we want to create LeagueController controller = new LeagueController(mock.Object, mockSeasonRepo.Object); // configuring the context for the controler fakeContext(controller); HttpResponseMessage response = controller.Get(1).Result; Assert.AreEqual(response.StatusCode, HttpStatusCode.NotFound); }
public async Task ReturnCorrectViewModel_WhenCalled() { // Arrange Mock <ILeagueService> leagueServiceMock = new Mock <ILeagueService>(); Mock <IPDFService> pDFServiceMock = new Mock <IPDFService>(); Mock <IMemoryCache> memoryCacheMock = new Mock <IMemoryCache>(); string validId = string.Empty; League validLeagueResult = new League(); leagueServiceMock.Setup(mock => mock.FindAsync(validId)) .Returns(Task.FromResult(validLeagueResult)); LeagueController SUT = new LeagueController( leagueServiceMock.Object, pDFServiceMock.Object, memoryCacheMock.Object); // Act var result = await SUT.Details(validId) as ViewResult; // Assert Assert.IsInstanceOfType(result.Model, typeof(LeagueDetailsViewModel)); }
public async Task CallFindAsync_WhenCalled() { // Arrange Mock <ILeagueService> leagueServiceMock = new Mock <ILeagueService>(); Mock <IPDFService> pDFServiceMock = new Mock <IPDFService>(); Mock <IMemoryCache> memoryCacheMock = new Mock <IMemoryCache>(); string validId = string.Empty; League validLeagueResult = new League(); leagueServiceMock.Setup(mock => mock.FindAsync(validId)) .Returns(Task.FromResult(validLeagueResult)); LeagueController SUT = new LeagueController( leagueServiceMock.Object, pDFServiceMock.Object, memoryCacheMock.Object); // Act await SUT.Details(validId); // Assert leagueServiceMock.Verify(mock => mock.FindAsync(validId), Times.Once); }
public void RetrieveAllleaguesInTheRepo() { IEnumerable <League> leagues = CreateLeagueList(); var mock = new Mock <ILeagueRepository>(MockBehavior.Strict); // Filling mock with data mock.As <ICRUDRepository <League, int, LeagueFilter> >().Setup(m => m.GetAll()) .Returns(Task.FromResult(leagues)); var mockSeasonRepo = new Mock <ISeasonRepository>(MockBehavior.Strict); mockSeasonRepo.As <ICRUDRepository <Season, int, SeasonFilter> >().Setup(m => m.Get(It.IsAny <int>())) .Returns <int>(id => Task.FromResult(new Season())); // Creating the controller which we want to create LeagueController controller = new LeagueController(mock.Object, mockSeasonRepo.Object); fakeContext(controller); HttpResponseMessage response = controller.GetAll().Result; Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); var objectContent = response.Content as ObjectContent; Assert.AreEqual(leagues, objectContent.Value); }
public async Task CallFilterLeaguesAsync_WhenCalled() { // Arrange Mock <ILeagueService> leagueServiceMock = new Mock <ILeagueService>(); Mock <IPDFService> pDFServiceMock = new Mock <IPDFService>(); Mock <IMemoryCache> memoryCacheMock = new Mock <IMemoryCache>(); string validSortOrder = string.Empty; string validFilter = string.Empty; int validPageNumber = 1; int validPageSize = 10; IPagedList <League> leagues = new PagedList <League>(new List <League>().AsQueryable(), validPageNumber, validPageSize); leagueServiceMock.Setup(mock => mock.FilterLeaguesAsync(validSortOrder, validFilter, validPageNumber, validPageSize)) .Returns(Task.FromResult(leagues)); LeagueController SUT = new LeagueController( leagueServiceMock.Object, pDFServiceMock.Object, memoryCacheMock.Object); // Act await SUT.Filter(validSortOrder, validFilter, validPageSize, validPageNumber); // Assert leagueServiceMock .Verify(mock => mock.FilterLeaguesAsync(validSortOrder, validFilter, validPageNumber, validPageSize), Times.Once); }
public void ValidOK() { //create an instance of the class we want to create LeagueController League = new LeagueController(); //test to see that it exists Assert.IsNotNull(League); }
public void Before() { _ctrl = new LeagueController() { Request = new HttpRequestMessage(), Configuration = new System.Web.Http.HttpConfiguration(), }; }
public async Task CallFilterLeaguesAsync_WhenCalled() { // Arrange Mock <ILeagueService> leagueServiceMock = new Mock <ILeagueService>(); Mock <IPDFService> pDFServiceMock = new Mock <IPDFService>(); Mock <IMemoryCache> memoryCacheMock = new Mock <IMemoryCache>(); IList <string> validFileParameters = typeof(LeagueDownloadViewModel).GetProperties().Select(p => p.Name.ToString()).ToList(); string validCollection = "leagues"; string validSortOrder = string.Empty; string validFilter = string.Empty; int validPageNumber = 1; int validPageSize = 10; string validFileName = string.Empty; byte[] validFileBytes = new byte[0]; IPagedList <League> leagues = new PagedList <League>( new List <League>().AsQueryable(), validPageNumber, validPageSize); IPagedList <LeagueDownloadViewModel> leagueDownloadViewModels = new PagedList <LeagueDownloadViewModel>( new List <LeagueDownloadViewModel>().AsQueryable(), validPageNumber, validPageSize); leagueServiceMock.Setup(mock => mock.FilterLeaguesAsync(validSortOrder, validFilter, validPageNumber, validPageSize)) .Returns(Task.FromResult(leagues)); pDFServiceMock .Setup(mock => mock.CreatePDF(leagueDownloadViewModels, validFileParameters, validCollection)) .Returns(validFileName); pDFServiceMock .Setup(mock => mock.GetFileBytesAsync(validFileName)) .Returns(Task.FromResult(validFileBytes)); pDFServiceMock .Setup(mock => mock.DeleteFile(validFileName)); LeagueController SUT = new LeagueController( leagueServiceMock.Object, pDFServiceMock.Object, memoryCacheMock.Object); // Act await SUT.Download(validSortOrder, validFilter, validPageSize, validPageNumber); // Assert leagueServiceMock.Verify(mock => mock.FilterLeaguesAsync(validSortOrder, validFilter, validPageNumber, validPageSize), Times.Once); }
public void LeagueController_Index_NoLeagues() { //Arrange var mockLeagueService = new MockLeagueService().MockGetAll(new List <League>()); var controller = new LeagueController(mockLeagueService.Object); //Act var result = controller.Index(); //Assert Assert.IsAssignableFrom <ViewResult>(result); mockLeagueService.VerifyGetAll(Times.Once()); }
public void RetrieveTopTen() { //Arrange var TestHelpers = new TestHelpers(); _players = TestHelpers.PopulateListOfPlayers(); var displayResults = _players.OrderByDescending(x => x.Wins).Take(10).ToList(); var mockLeagueServices = new Mock <ILeagueService>(); mockLeagueServices.Setup(t => t.GetTopTenPlayers()).Returns(displayResults); var leagueController = new LeagueController(mockLeagueServices.Object); //Act var returnedTopTen = leagueController.GetTopTen(); var okObjectResult = returnedTopTen as OkObjectResult; //Assert okObjectResult.Value.ToString().Should().Contain(displayResults[0].Id.ToString()); }
public async Task ThrowApplicationException_WhenPassedNullId() { // Arrange Mock <ILeagueService> leagueServiceMock = new Mock <ILeagueService>(); Mock <IPDFService> pDFServiceMock = new Mock <IPDFService>(); Mock <IMemoryCache> memoryCacheMock = new Mock <IMemoryCache>(); string validId = string.Empty; League validLeagueResult = new League(); leagueServiceMock.Setup(mock => mock.FindAsync(validId)) .Returns(Task.FromResult(validLeagueResult)); LeagueController SUT = new LeagueController( leagueServiceMock.Object, pDFServiceMock.Object, memoryCacheMock.Object); // Act & Assert await Assert.ThrowsExceptionAsync <ApplicationException>(() => SUT.Details(null)); }
public void LeagueController_Index_LeaguesExist() { //Arrange var mockLeagues = new List <League>() { new League() { ID = 1, FoundingDate = new DateTime(1933, 5, 3) } }; var mockLeagueService = new MockLeagueService().MockGetAll(mockLeagues); var controller = new LeagueController(mockLeagueService.Object); //Act var result = controller.Index(); //Assert Assert.IsAssignableFrom <ViewResult>(result); mockLeagueService.VerifyGetAll(Times.Once()); }
public async void TestForCreateLeague() { var options = new DbContextOptionsBuilder <LeagueContext>() .UseInMemoryDatabase(databaseName: "p3LeagueControllerCreateLeague") .Options; using (var context = new LeagueContext(options)) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); Repo r = new Repo(context, new NullLogger <Repo>()); Logic logic = new Logic(r, new NullLogger <Repo>()); LeagueController leagueController = new LeagueController(logic); var sport = new Sport { SportID = 35, SportName = "basketball" }; r.Sports.Add(sport); await r.CommitSave(); var leagueDto = new CreateLeagueDto { LeagueName = "sports", SportName = sport.SportName }; var createLeague = await leagueController.CreateLeague(leagueDto); Assert.IsAssignableFrom <League>((createLeague as OkObjectResult).Value); var createLeague2 = await leagueController.CreateLeague(leagueDto); Assert.IsAssignableFrom <string>((createLeague2 as ConflictObjectResult).Value); } }
public void DeleteFailureLeagueInTheRepo() { var mock = new Mock <ILeagueRepository>(MockBehavior.Strict); // Creating the rules for mock, always send true in this case mock.As <ICRUDRepository <League, int, LeagueFilter> >().Setup(m => m.Remove(It.IsAny <int>())) .Returns(Task.FromResult(false)); var mockSeasonRepo = new Mock <ISeasonRepository>(MockBehavior.Strict); mockSeasonRepo.As <ICRUDRepository <Season, int, SeasonFilter> >().Setup(m => m.Get(It.IsAny <int>())) .Returns <int>(id => Task.FromResult(new Season())); // Creating the controller which we want to create LeagueController controller = new LeagueController(mock.Object, mockSeasonRepo.Object); // configuring the context for the controler fakeContext(controller); HttpResponseMessage response = controller.Delete(0).Result; // the result should say "HttpStatusCode.NotFound" Assert.AreEqual(response.StatusCode, HttpStatusCode.NotFound); }
public void UpdateLeagueInTheRepo() { League league = new League(); var mock = new Mock <ILeagueRepository>(MockBehavior.Strict); // Creating the rules for mock, always send true in this case mock.As <ICRUDRepository <League, int, LeagueFilter> >().Setup(m => m.Update(It.IsAny <int>(), It.IsAny <League>())) .Returns(Task.FromResult(true)); mock.As <ILeagueRepository>().Setup(m => m.isLeagueNameExist(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <int>())) .Returns(Task.FromResult(false)); var mockSeasonRepo = new Mock <ISeasonRepository>(MockBehavior.Strict); mockSeasonRepo.As <ICRUDRepository <Season, int, SeasonFilter> >().Setup(m => m.Get(It.IsAny <int>())) .Returns <int>(id => Task.FromResult(new Season())); // Creating the controller which we want to create LeagueController controller = new LeagueController(mock.Object, mockSeasonRepo.Object); // configuring the context for the controler fakeContext(controller); League modifiedleague = new League(); modifiedleague.Id = league.Id; modifiedleague.Name = "ModifiedName"; HttpResponseMessage response = controller.Put(modifiedleague.Id, modifiedleague).Result; // the result should say "HttpStatusCode.Created" and the returned object should have a different lastName Assert.AreEqual(response.StatusCode, HttpStatusCode.Created); var objectContent = response.Content as ObjectContent; Assert.AreNotEqual(league.Name, ((League)objectContent.Value).Name); }
public void SetUp() { context = new MockDreamLeagueContext(); controller = new LeagueController(context.MockContext.Object); }
private void Awake() { instance = this; CreateLeague(); }
public LeagueControllerTest() { _mockLeagueRepository = DataHelper.GetTestLeagueRepository(); _leagueController = new LeagueController(_mockLeagueRepository.Object); }
public static void MyClassInitialize(TestContext testContext) { controller = new LeagueController(new UnitOfWork(new BruceFoxContext())); }