Пример #1
0
        public Tweet CreateFromApiDto(TweetFromApiDto tweet)
        {
            if (tweet == null)
            {
                throw new ArgumentNullException("Tweet cannot be null!");
            }

            var tweetToAdd = mapper.MapTo <Tweet>(tweet);

            this.unitOfWork.Tweets.Add(tweetToAdd);
            this.unitOfWork.SaveChanges();
            return(tweetToAdd);
        }
        public void Invokes_SaveChanges_When_Tweet_Is_Created()
        {
            //Arrange
            var unitOfWorkMock            = new Mock <IUnitOfWork>();
            var mapperMock                = new Mock <IMappingProvider>();
            var twitterApiCallServiceMock = new Mock <ITwitterApiCallService>();
            var tweetTagServiceMock       = new Mock <ITweetTagService>();
            var tagServiceMock            = new Mock <ITagService>();
            var dateTimeParserMock        = new Mock <IDateTimeParser>();
            var repoMock = new Mock <IGenericRepository <Tweet> >();

            var tweetDtoFromApi = new TweetFromApiDto
            {
                TweetId = "1",
                Text    = "Tweet text",
                OriginalTweetCreatedOn = "01/05/2005",
            };

            var tweet = new Tweet
            {
                TweetId = tweetDtoFromApi.TweetId,
                Text    = tweetDtoFromApi.Text,
                OriginalTweetCreatedOn = DateTime.Parse(tweetDtoFromApi.OriginalTweetCreatedOn),
                FolloweeId             = "1"
            };

            var tweetCollection = new List <Tweet> {
                tweet
            };

            repoMock.Setup(r => r.AllAndDeleted).Returns(tweetCollection.AsQueryable());
            unitOfWorkMock.Setup(u => u.Tweets).Returns(repoMock.Object);
            unitOfWorkMock.Setup(u => u.Tweets.Add(It.IsAny <Tweet>())).Verifiable();

            var sut = new TweetService(mapperMock.Object, unitOfWorkMock.Object,
                                       twitterApiCallServiceMock.Object, tweetTagServiceMock.Object,
                                       tagServiceMock.Object, dateTimeParserMock.Object);

            //Act
            sut.CreateFromApiDto(tweetDtoFromApi);

            //Assert
            unitOfWorkMock.Verify(v => v.SaveChanges(), Times.Once);
        }