Exemplo n.º 1
0
        public void CreateNewRatingWithCorrectProperties_WhenRatingNotFound()
        {
            // Arrange
            var mockedData = new Mock <IBetterReadsData>();
            var rating1    = new Rating()
            {
                UserId = "user-id-1", BookId = 1234
            };
            var rating2 = new Rating()
            {
                UserId = "user-id-2", BookId = 2345
            };
            var ratings = new List <Rating>()
            {
                rating1,
                rating2
            }.AsQueryable();

            Rating addedRating = null;

            mockedData.Setup(x => x.Ratings.All).Returns(ratings);
            mockedData.Setup(x => x.Ratings.Add(It.IsAny <Rating>()))
            .Callback((Rating r) => addedRating = r);

            var service = new RatingsService(mockedData.Object);

            // Act
            service.RateBook(1, "radom-id", 5);

            // Assert
            mockedData.Verify(x => x.Ratings.Add(It.IsAny <Rating>()), Times.Once);
            Assert.AreEqual(1, addedRating.BookId);
            Assert.AreEqual("radom-id", addedRating.UserId);
            Assert.AreEqual(5, addedRating.Value);
        }
Exemplo n.º 2
0
        public void CreateNewRating_WhenRatingNotFound()
        {
            // Arrange
            var mockedData = new Mock <IBetterReadsData>();
            var rating1    = new Rating()
            {
                UserId = "user-id-1", BookId = 1234
            };
            var rating2 = new Rating()
            {
                UserId = "user-id-2", BookId = 2345
            };
            var ratings = new List <Rating>()
            {
                rating1,
                rating2
            }.AsQueryable();

            mockedData.Setup(x => x.Ratings.All).Returns(ratings);
            mockedData.Setup(x => x.Ratings.Add(It.IsAny <Rating>())).Verifiable();

            var service = new RatingsService(mockedData.Object);

            // Act
            service.RateBook(1, "radom-id", 5);

            // Assert
            mockedData.Verify(x => x.Ratings.Add(It.IsAny <Rating>()), Times.Once);
        }
Exemplo n.º 3
0
        public void ChangeRatingValue_WhenRatingFound()
        {
            // Arrange
            var userId     = "user-id-12424";
            var bookId     = 24147;
            int rateValue  = 5;
            var mockedData = new Mock <IBetterReadsData>();
            var rating1    = new Rating()
            {
                UserId = userId, BookId = bookId
            };
            var rating2 = new Rating()
            {
                UserId = userId, BookId = 2345
            };
            var ratings = new List <Rating>()
            {
                rating1,
                rating2
            }.AsQueryable();

            mockedData.Setup(x => x.Ratings.All).Returns(ratings);

            var service = new RatingsService(mockedData.Object);

            // Act
            service.RateBook(bookId, userId, rateValue);

            // Assert
            Assert.AreEqual(rateValue, rating1.Value);
        }
Exemplo n.º 4
0
        public void CallDataSaveChanges_WhenRatingFound()
        {
            // Arrange
            var userId     = "user-id-12424";
            var bookId     = 24147;
            var mockedData = new Mock <IBetterReadsData>();

            var rating1 = new Rating()
            {
                UserId = userId, BookId = bookId
            };
            var ratings = new List <Rating>()
            {
                rating1
            }.AsQueryable();

            mockedData.Setup(x => x.Ratings.All).Returns(ratings);
            mockedData.Setup(x => x.SaveChanges()).Verifiable();

            var service = new RatingsService(mockedData.Object);

            // Act
            service.RateBook(bookId, userId, 5);

            // Assert
            mockedData.Verify(x => x.SaveChanges(), Times.Once);
        }
Exemplo n.º 5
0
        public void CallDataSaveChanges_WhenRatingNotFound()
        {
            // Arrange
            var mockedData = new Mock <IBetterReadsData>();

            mockedData.Setup(x => x.Ratings.All).Returns(new List <Rating>().AsQueryable());
            mockedData.Setup(x => x.SaveChanges()).Verifiable();

            var service = new RatingsService(mockedData.Object);

            // Act
            service.RateBook(1, "radom-id", 5);

            // Assert
            mockedData.Verify(x => x.SaveChanges(), Times.Once);
        }