コード例 #1
0
        public void RatingsCrud()
        {
            var options = CreateOptions("RatingsCrud");

            using (var context = new ApplicationDbContext((options)))
            {
                var controller =
                    new ProductsController(
                        new ProductsService(
                            context));

                Assert.That(
                    controller.ListProducts(0).Count == 0,
                    "Test database should be empty, but found at least one product");

                var product = new Product()
                {
                    Name        = "Rated Product Test",
                    Description = "Rated Product Test"
                };
                context.Products.Add(product);

                var users = new List <ApplicationUser>()
                {
                    new ApplicationUser()
                    {
                        UserName = "******"
                    },
                    new ApplicationUser()
                    {
                        UserName = "******"
                    }
                };
                context.Users.AddRange(users);
                context.SaveChanges();
            }

            using (var context = new ApplicationDbContext(options))
            {
                var controller =
                    new ProductsController(
                        new ProductsService(
                            context));

                // Default identity to avoid NullReferenceException
                controller.ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                    {
                        User = new ClaimsPrincipal(new ClaimsIdentity())
                    }
                };

                var id = context.Products.First().Id;
                var productViewModel = controller.GetProductWithRatings(id, 0);
                Assert.That(
                    productViewModel.AverageRating == 0,
                    "Product has no ratings, but a non-zero average rating.");
                Assert.That(productViewModel.UserRating == 0,
                            "User has not rated product, but user rating is reported to be non-zero.");

                // "Logging in" user
                controller.ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                    {
                        User = new ClaimsPrincipal(
                            new ClaimsIdentity(
                                new Claim[]
                        {
                            new Claim(ClaimTypes.Name, "Alice")
                        }))
                    }
                };


                controller.AddRating(new RatingViewModel {
                    Message   = "Test rating",
                    ProductId = id,
                    UserName  = "******",
                    Value     = 5
                });

                productViewModel = controller.GetProductWithRatings(id, 0);
                Assert.That(
                    productViewModel.UserRating == 5,
                    "User has given product rating value of 5, but UserRating is not 5.");
                Assert.That(productViewModel.AverageRating == 5,
                            "Product has only one rating with a value of 5, but AverageRating is not 5.");

                // "Log in" as different user
                controller.ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                    {
                        User = new ClaimsPrincipal(
                            new ClaimsIdentity(
                                new Claim[]
                        {
                            new Claim(ClaimTypes.Name, "Bob")
                        }))
                    }
                };

                productViewModel = controller.GetProductWithRatings(id, 0);
                Assert.That(
                    productViewModel.UserRating == 0,
                    "User has not rated product, but user rating is reported to be non-zero.");
                Assert.That(productViewModel.AverageRating == 5,
                            "Product has only one rating with a value of 5, but AverageRating is not 5.");

                controller.AddRating(new RatingViewModel
                {
                    Message   = "Test rating",
                    ProductId = id,
                    UserName  = "******",
                    Value     = 4
                });

                productViewModel = controller.GetProductWithRatings(id, 0);
                Assert.That(productViewModel.UserRating == 4,
                            "User has given product rating value of 4, but UserRating is not 4");
                Assert.That(productViewModel.AverageRating == 4.5,
                            "Product has two ratings total, with values of 4 and 5, but the product's" +
                            "average rating is not reported as 4.5.");

                // Log out
                controller.ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                };

                productViewModel = controller.GetProductWithRatings(id, 0);
                Assert.That(
                    productViewModel.UserRating == 0,
                    "User is not logged in, but product's user rating is reported as non-zero");
                Assert.That(productViewModel.AverageRating == 4.5,
                            "Product has two ratings total, with values of 4 and 5, but the product's" +
                            "average rating is not reported as 4.5.");
            }
        }