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 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 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); }