예제 #1
0
        public void DoNotHandleUserLeftWhenUserHaveOtherConnections()
        {
            //assert
            var options     = Utils.GetDbOptions("RemoveUserConnectionShould_DoNotHandleUserLeftWhenUserHaveOtherConnections");
            var chatService = new Mock <IChatService>();

            using (var context = new ApplicationDbContext(options))
            {
                context.Users.Add(new User()
                {
                    Id = "test_user"
                });
                context.Lobbies.Add(new Lobby()
                {
                    ID = 5
                });
                context.UserParticipationInLobbies.Add(new UserParticipationInLobby()
                {
                    UserID        = "test_user",
                    LobbyID       = 5,
                    ConnectionIds = new HashSet <string> {
                        "otherConnectionId", "connectionId"
                    }
                });
                context.SaveChanges();
            }
            //act
            using (var context = new ApplicationDbContext(options))
            {
                var service = new LobbyService(context, null, chatService.Object);
                service.RemoveUserConnection("test_user", "connectionId");
            }
            //assert
            chatService.Verify(cs => cs.OnUserLeft(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
        }
예제 #2
0
        public void ThrowExceptionWhenUserNotFound()
        {
            //assert
            var options     = Utils.GetDbOptions("RemoveUserConnectionShould_ThrowExceptionWhenUserNotFound");
            var chatService = new Mock <IChatService>();

            //act & assert
            using (var context = new ApplicationDbContext(options))
            {
                var service = new LobbyService(context, null, chatService.Object);
                Assert.Throws <ArgumentOutOfRangeException>(() => service.RemoveUserConnection("ghost_user", "connectionId"));
            }
        }
예제 #3
0
        public void RemoveConnectionWhenUserParticipatesInLobby()
        {
            //assert
            var options     = Utils.GetDbOptions("RemoveUserConnectionShould_RemoveConnectionWhenUserParticipatesInLobby");
            var chatService = new Mock <IChatService>();

            using (var context = new ApplicationDbContext(options))
            {
                context.Users.Add(new User()
                {
                    Id = "test_user"
                });
                context.Lobbies.Add(new Lobby()
                {
                    ID = 5
                });
                context.UserParticipationInLobbies.Add(new UserParticipationInLobby()
                {
                    UserID        = "test_user",
                    LobbyID       = 5,
                    ConnectionIds = new HashSet <string> {
                        "connectionId"
                    }
                });
                context.SaveChanges();
            }
            //act
            using (var context = new ApplicationDbContext(options))
            {
                var service = new LobbyService(context, null, chatService.Object);
                service.RemoveUserConnection("test_user", "connectionId");
            }
            //assert
            using (var context = new ApplicationDbContext(options))
            {
                var connections = context.UserParticipationInLobbies.Find(new object[] { 5, "test_user" }).ConnectionIds;
                Assert.Equal(0, connections.Count);
                chatService.Verify(cs => cs.RemoveConnectionsFromChat(
                                       It.IsAny <ICollection <string> >(),
                                       It.Is <string>(chatId => chatId == "5")));
            }
        }
예제 #4
0
        public void ThrowExceptionWhenUserDoesNotParticipateInAnyLobby()
        {
            //assert
            var options     = Utils.GetDbOptions("RemoveUserConnectionShould_ThrowExceptionWhenUserDoesNotParticipateInAnyLobby");
            var chatService = new Mock <IChatService>();

            using (var context = new ApplicationDbContext(options))
            {
                context.Users.Add(new User()
                {
                    Id = "test_user"
                });
                context.SaveChanges();
            }
            //act & assert
            using (var context = new ApplicationDbContext(options))
            {
                var service = new LobbyService(context, null, chatService.Object);
                Assert.Throws <InvalidOperationException>(() => service.RemoveUserConnection("test_user", "connectionId"));
            }
        }
예제 #5
0
        public void HandleUserLeftWhenRemovedLastConnection()
        {
            //assert
            var options     = Utils.GetDbOptions("RemoveUserConnectionShould_HandleUserLeftWhenRemovedLastConnection");
            var chatService = new Mock <IChatService>();

            using (var context = new ApplicationDbContext(options))
            {
                context.Users.Add(new User()
                {
                    Id       = "test_user",
                    UserName = "******"
                });
                context.Lobbies.Add(new Lobby()
                {
                    ID = 5
                });
                context.UserParticipationInLobbies.Add(new UserParticipationInLobby()
                {
                    UserID        = "test_user",
                    LobbyID       = 5,
                    ConnectionIds = new HashSet <string> {
                        "connectionId"
                    }
                });
                context.SaveChanges();
            }
            //act
            using (var context = new ApplicationDbContext(options))
            {
                var service = new LobbyService(context, null, chatService.Object);
                service.RemoveUserConnection("test_user", "connectionId");
            }
            //assert
            chatService.Verify(cs => cs.OnUserLeft(
                                   It.Is <string>(username => username == "TestUser"),
                                   It.Is <string>(chatId => chatId == "5")));
        }