コード例 #1
0
        public async Task <RestaurantReview> PostReview([FromBody] RestaurantReview review)
        {
            RestaurantReview savedReview = null;

            using (RestaurantReviewsUnitOfWork uow = new RestaurantReviewsUnitOfWork(dbPath))
            {
                var _results = await uow.AddRestaurantReviewAsync(review);

                if (_results.Successful)
                {
                    savedReview = _results.Results;
                }
                else
                {
                    // here we can add special handling in the case of failure
                }
            }

            return(savedReview);
        }
コード例 #2
0
        public async Task AddRestaurantReviewAsyncTests()
        {
            RestaurantAddress address = new RestaurantAddress()
            {
                streetAddress = "100 Main St",
                addrLine2     = string.Empty,
                city          = "Pittsburgh",
                state         = "PA",
                zipcode       = "15108",
                dateCreated   = DateTime.Now,
                dateModified  = DateTime.Now
            };

            Restaurant restaurant = new Restaurant()
            {
                name                = "Restaurant 1",
                address             = address,
                addressId           = address.id,
                dateCreated         = DateTime.Now,
                dateModified        = DateTime.Now,
                phoneNumber         = "412-444-4444",
                acceptsReservations = false
            };

            RestaurantReview review1 = new RestaurantReview()
            {
                restaurant   = restaurant,
                restaurantId = restaurant.id,
                reviewDate   = DateTime.Now,
                reviewText   = "This restaurant served us quickly and the food was great!",
                userRating   = 5,
                dateCreated  = DateTime.Now,
                dateModified = DateTime.Now,
                username     = "******"
            };

            // Test the 'Happy Path'
            repositoryMock.Setup(m => m.AddUpdateReviewAsync(It.IsAny <RestaurantReview>())).ReturnsAsync(review1);
            using (RestaurantReviewsUnitOfWork uow = new RestaurantReviewsUnitOfWork(repositoryMock.Object))
            {
                var _results = await uow.AddRestaurantReviewAsync(review1);

                Assert.IsNotNull(_results);
                Assert.IsNotNull(_results.Results);
                Assert.IsInstanceOfType(_results.Results, typeof(RestaurantReview));
                Assert.AreEqual(_results.Results.id, review1.id);
            }

            // Test NULL review
            Exception exNullReference = new NullReferenceException();

            repositoryMock.Setup(m => m.AddUpdateReviewAsync(It.IsAny <RestaurantReview>())).ThrowsAsync(exNullReference);
            using (RestaurantReviewsUnitOfWork uow = new RestaurantReviewsUnitOfWork(repositoryMock.Object))
            {
                var _results = await uow.AddRestaurantReviewAsync(null);

                Assert.IsNotNull(_results);
                Assert.IsNotNull(_results.Exception);
                Assert.IsNull(_results.Results);
                Assert.IsFalse(_results.Successful);
            }

            // Test Exception Handling
            Exception ex = new Exception("Any Exception");

            repositoryMock.Setup(m => m.AddUpdateReviewAsync(It.IsAny <RestaurantReview>())).ThrowsAsync(ex);
            using (RestaurantReviewsUnitOfWork uow = new RestaurantReviewsUnitOfWork(repositoryMock.Object))
            {
                var _results = await uow.AddRestaurantReviewAsync(review1);

                Assert.IsNotNull(_results);
                Assert.IsNotNull(_results.Exception);
                Assert.IsNull(_results.Results);
                Assert.IsFalse(_results.Successful);
            }
        }