예제 #1
0
        public void AcceptFriendship_WhenException_ShouldRollback()
        {
            var firstUser  = ModelTestHelper.CreateUser(-1, "aUser");
            var secondUser = ModelTestHelper.CreateUser(-1, "aFriend", "secondEmail");
            var userRepo   = new UserRepository();

            userRepo.SaveUser(firstUser);
            Assert.IsFalse(userRepo.HasErrors);
            userRepo.SaveUser(secondUser);
            Assert.IsFalse(userRepo.HasErrors);
            var friendship = ModelTestHelper.CreateFriendship(firstUser.Id, secondUser.Pseudo, DateTime.Now, true, true);
            var service    = new FriendshipServices();

            service.RequestFriendship(friendship, firstUser.Pseudo);
            Assert.IsFalse(service.HasErrors);
            var mock = new Mock <IUserRepository>();

            mock.SetupGet(u => u.HasErrors).Throws(new Exception("Rollback"));
            var mockService = new FriendshipServices(mock.Object, new FriendshipRepository());

            mockService.AcceptFriendship(friendship, firstUser.Pseudo);
            Assert.IsTrue(mockService.HasErrors);
            var dbFriendship = service.GetFriendship(friendship.UserId, friendship.FriendName);

            Assert.IsTrue(dbFriendship.IsWaiting);
        }
예제 #2
0
        public void DenyFriendship_WhenError_ShouldRollback()
        {
            var firstUser  = ModelTestHelper.CreateUser(-1, "aUser");
            var secondUser = ModelTestHelper.CreateUser(-1, "aFriend", "secondEmail");
            var userRepo   = new UserRepository();

            userRepo.SaveUser(firstUser);
            Assert.IsFalse(userRepo.HasErrors);
            userRepo.SaveUser(secondUser);
            Assert.IsFalse(userRepo.HasErrors);
            var friendship = ModelTestHelper.CreateFriendship(firstUser.Id, secondUser.Pseudo, DateTime.Now, true, true);
            var service    = new FriendshipServices();

            service.RequestFriendship(friendship, firstUser.Pseudo);
            Assert.IsFalse(service.HasErrors);
            var mock = new Mock <IUserRepository>();

            mock.SetupGet(u => u.HasErrors).Returns(false);
            mock.Setup(s => s.GetUserInfo(It.IsAny <string>())).Returns(() => null);
            var mockService = new FriendshipServices(mock.Object, new FriendshipRepository());

            mockService.DenyFriendship(friendship, firstUser.Pseudo);
            Assert.IsTrue(mockService.HasErrors);
            var dbFriendship = service.GetFriendship(friendship.UserId, friendship.FriendName);

            Assert.IsNotNull(dbFriendship);
        }
예제 #3
0
        public void AcceptFriendship_WhenValid_ShouldCommit()
        {
            var firstUser  = ModelTestHelper.CreateUser(-1, "aUser");
            var secondUser = ModelTestHelper.CreateUser(-1, "aFriend", "secondEmail");
            var userRepo   = new UserRepository();

            userRepo.SaveUser(firstUser);
            Assert.IsFalse(userRepo.HasErrors);
            userRepo.SaveUser(secondUser);
            Assert.IsFalse(userRepo.HasErrors);
            var friendship = ModelTestHelper.CreateFriendship(firstUser.Id, secondUser.Pseudo, DateTime.Now, true, true);
            var service    = new FriendshipServices();

            service.RequestFriendship(friendship, firstUser.Pseudo);
            Assert.IsFalse(service.HasErrors);
            service.AcceptFriendship(friendship, firstUser.Pseudo);
            Assert.IsFalse(service.HasErrors);
            var dbFriendship = service.GetFriendship(friendship.UserId, friendship.FriendName);

            Assert.IsNotNull(dbFriendship);
            Assert.IsFalse(dbFriendship.IsWaiting);
            var otherFriendship = service.GetFriendship(secondUser.Id, firstUser.Pseudo);

            Assert.IsNotNull(otherFriendship);
            Assert.IsFalse(dbFriendship.IsWaiting);
        }
예제 #4
0
        public void RequestFriendship_WhenException_ShouldRollback()
        {
            var friendship     = ModelTestHelper.CreateFriendship(1, "aFriend");
            var friendshipRepo = new FriendshipRepository();
            var mock           = new Mock <IUserRepository>();

            mock.SetupGet(s => s.HasErrors).Throws(new Exception("For Rollback"));
            var service = new FriendshipServices(mock.Object, friendshipRepo);

            service.RequestFriendship(friendship, "aFriend");
            Assert.IsNull(friendshipRepo.GetFriendship(friendship.UserId, friendship.FriendName));
        }
예제 #5
0
        public void RequestFriendship_WhenErrorDuringSave_ShouldRollback()
        {
            var friendship     = ModelTestHelper.CreateFriendship(1, "aFriend");
            var friendshipRepo = new FriendshipRepository();
            var mock           = new Mock <IUserRepository>();

            mock.SetupGet(s => s.HasErrors).Returns(true);
            mock.Setup(s => s.GetUserInfo(It.IsAny <string>())).Returns(() => null);
            var service = new FriendshipServices(mock.Object, friendshipRepo);

            service.RequestFriendship(friendship, "aFriend");
            Assert.IsNull(friendshipRepo.GetFriendship(friendship.UserId, friendship.FriendName));
        }