public void SubscribeFromTo(VidconfileUser from, VidconfileUser to) { if (from == null) { throw new NullReferenceException("from cannot be null"); } if (to == null) { throw new NullReferenceException("to cannot be null"); } if (this.IsSubscribed(from, to)) { throw new ArgumentException("the user is already subscribed"); } var subscriberToSubscribers = new SubscribeToSubscribers(); subscriberToSubscribers.SubscriberId = from.Id; subscriberToSubscribers.SubscribedToId = to.Id; this.subscribeToSubscribersRepository.Add(subscriberToSubscribers); this.unitOfWork.Commit(); }
public void SubscribeFromTo_ShouldSubscribe() { var videoRepositoryMock = new Mock <IRepository <Video> >(); var unitOfWorkMock = new Mock <IUnitOfWork>(); var commentRepositoryMock = new Mock <IRepository <Comment> >(); var userRepositoryMock = new Mock <IRepository <VidconfileUser> >(); var passwordHasherMock = new Mock <IPasswordHasher>(); var subscribeToSubscriberMock = new Mock <IRepository <SubscribeToSubscribers> >(); Guid fromId = Guid.NewGuid(); Guid toId = Guid.NewGuid(); VidconfileUser from = new VidconfileUser(); VidconfileUser to = new VidconfileUser(); from.Id = fromId; to.Id = toId; var sb = new SubscribeToSubscribers(); sb.SubscribedToId = toId; sb.SubscriberId = Guid.Empty; SubscribeToSubscribers res = null; subscribeToSubscriberMock.Setup(x => x.All()) .Returns(new List <SubscribeToSubscribers>() { sb }.AsQueryable()); subscribeToSubscriberMock.Setup(x => x.Add(It.IsAny <SubscribeToSubscribers>())) .Callback <SubscribeToSubscribers>(x => res = x) .Verifiable(); VidconfileUserServices userService = new VidconfileUserServices(userRepositoryMock.Object, unitOfWorkMock.Object, passwordHasherMock.Object, videoRepositoryMock.Object, subscribeToSubscriberMock.Object); userService.SubscribeFromTo(from, to); subscribeToSubscriberMock.Verify(); unitOfWorkMock.Verify(x => x.Commit(), Times.Once); Assert.Equal(toId, res.SubscribedToId); Assert.Equal(fromId, res.SubscriberId); }
public void UnsubscribeFromTo_AlreadySubscribed_ShouldThrow() { var videoRepositoryMock = new Mock <IRepository <Video> >(); var unitOfWorkMock = new Mock <IUnitOfWork>(); var commentRepositoryMock = new Mock <IRepository <Comment> >(); var userRepositoryMock = new Mock <IRepository <VidconfileUser> >(); var passwordHasherMock = new Mock <IPasswordHasher>(); var subscribeToSubscriberMock = new Mock <IRepository <SubscribeToSubscribers> >(); Guid fromId = Guid.NewGuid(); Guid toId = Guid.NewGuid(); VidconfileUser from = new VidconfileUser(); VidconfileUser to = new VidconfileUser(); from.Id = fromId; to.Id = toId; var sb = new SubscribeToSubscribers(); sb.SubscribedToId = toId; sb.SubscriberId = Guid.Empty; subscribeToSubscriberMock.Setup(x => x.All()) .Returns(new List <SubscribeToSubscribers>() { sb }.AsQueryable()); VidconfileUserServices userService = new VidconfileUserServices(userRepositoryMock.Object, unitOfWorkMock.Object, passwordHasherMock.Object, videoRepositoryMock.Object, subscribeToSubscriberMock.Object); string msg = Assert.Throws <ArgumentException>(() => userService.UnsubscribeFromTo(from, to)) .Message; Assert.Equal("the user is not subscribed", msg); }