[Test, Isolated] // use 'Isolated' attr. because this test changes the state of the db & we want to make sure the changes are rolled back. public void Mine_WhenCalled_ShouldReturnUpcomingCoops() { // Arrange // Mock the current user (w/ extension method) var user = _context.Users.First(); _controller.MockCurrentUser(user.Id, user.UserName); var game = _context.Games.First(); var coop = new Coop { Host = user, DateTime = DateTime.Now.AddDays(1), Game = game, Venue = "-" }; // Add co-op session to context _context.Coops.Add(coop); _context.SaveChanges(); // Act var result = _controller.Mine(); // Assert (result.ViewData.Model as IEnumerable <Coop>).Should().HaveCount(1); }
public void TestInitialize() { // Create mock repository - will be empty unless setup in test methods below _mockRepository = new Mock <ICoopRepository>(); // Create a moq unit of work var mockUoW = new Mock <IUnitOfWork>(); // because we are unit testing the controller, we don't want to couple to the real UnitOfWork which uses EntityFramework to talk to the db mockUoW.SetupGet(u => u.Coops).Returns(_mockRepository.Object); // * --> when we access the Coops property of UoW, we get the mock repository * // Create the controller (*references the Api controller) _controller = new CoopsController(mockUoW.Object); // the Object property is the actual mock implementation of IUnitOfWork // Set controller user w/ the generic principal object by calling extension method, passing in the userid/username we want to use _userId = "1"; _controller.MockCurrentUser(_userId, "*****@*****.**"); }
[Test, Isolated] // use 'Isolated' attr. because this test changes the state of the db & we want to make sure the changes are rolled back. public void Cancel_WhenCalled_ShouldCancelTheGivenCoop() { // Arrange // Mock the current user (w/ extension method) **NOTE - TODO: move the controller extension methods to a separate, shared library so all test projects can reference a single .dll var user = _context.Users.First(); _controller.MockCurrentUser(user.Id, user.UserName); // Add co-op session to context var game = _context.Games.Single(g => g.Id == 1); // get a game that we know the Id for var coop = new Coop { Host = user, DateTime = DateTime.Now.AddDays(1), Game = game, Venue = "-" }; _context.Coops.Add(coop); _context.SaveChanges(); // Act var result = _controller.Cancel(coop.Id); // Assert _context.Entry(coop).Reload(); // refresh the canceled co-op session from the database coop.IsCanceled.Should().BeTrue(); }