public async void Add_IdInDb_RedirectsToIndex(bool itemAlreadyInWishlist) { Guid gameProductId = new Guid("976ACE77-D87C-4EBE-83A0-46F911F6490E"); PhysicalGameProduct gameProduct = new PhysicalGameProduct() { Game = new Game() { Name = "TestGame" }, SKUNameSuffix = "TestGameProductName", Id = gameProductId }; Member currentMember = new Member { UserId = UserId, Wishlist = new List<Product>() }; if (itemAlreadyInWishlist) { currentMember.Wishlist.Add(gameProduct); } Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake(); Mock<DbSet<Product>> productDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Product>().AsQueryable()); productDbSetStub.Setup(db => db.FindAsync(gameProduct.Id)).ReturnsAsync(gameProduct); dbStub.Setup(db => db.Products).Returns(productDbSetStub.Object); Mock<DbSet<Member>> memberDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Member> { currentMember }.AsQueryable()); memberDbSetStub.Setup(db => db.FindAsync(currentMember.UserId)).ReturnsAsync(currentMember); dbStub.Setup(db => db.Members).Returns(memberDbSetStub.Object); Mock<ControllerContext> context = new Mock<ControllerContext>(); context.Setup(c => c.HttpContext.User.Identity).Returns<IIdentity>(null); Mock<IGuidUserIdGetter> idGetterStub = new Mock<IGuidUserIdGetter>(); idGetterStub.Setup(id => id.GetUserId(It.IsAny<IIdentity>())).Returns(currentMember.UserId); WishlistController controller = new WishlistController(dbStub.Object, idGetterStub.Object) { ControllerContext = context.Object }; var result = await controller.Add(gameProduct.Id) as RedirectToRouteResult; Assert.That(result != null); Assert.That(result.RouteValues["action"], Is.EqualTo("Index")); Assert.That(currentMember.Wishlist.Contains(gameProduct)); }
public void Add_IdNotInDb_Throws404Exception() { Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake(); Mock<DbSet<Product>> productDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Product>().AsQueryable()); dbStub.Setup(db => db.Products).Returns(productDbSetStub.Object); WishlistController controller = new WishlistController(dbStub.Object, idGetter: null); Assert.That(async () => await controller.Add(Id), Throws.InstanceOf<HttpException>().And.Matches<HttpException>(ex => ex.GetHttpCode() == 404)); }