예제 #1
0
        public void DoNotHandleUserJoinWhenUserHadConnectionBefore()
        {
            //assert
            var options     = Utils.GetDbOptions("SaveUserConnectionShould_DoNotHandleUserJoinWhenUserHadConnectionBefore");
            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"
                    }
                });
                context.SaveChanges();
            }
            //act
            using (var context = new ApplicationDbContext(options))
            {
                var service = new LobbyService(context, null, chatService.Object);
                service.SaveUserConnection("test_user", "connectionId");
            }
            //assert
            chatService.Verify(cs => cs.OnUserJoined(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
        }
예제 #2
0
        public void HandleUserJoinWhenAddedFirstConnection()
        {
            //assert
            var options     = Utils.GetDbOptions("SaveUserConnectionShould_HandleUserJoinWhenAddedFirstConnection");
            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
                });
                context.SaveChanges();
            }
            //act
            using (var context = new ApplicationDbContext(options))
            {
                var service = new LobbyService(context, null, chatService.Object);
                service.SaveUserConnection("test_user", "connectionId");
            }
            //assert
            chatService.Verify(cs => cs.OnUserJoined(
                                   It.Is <string>(username => username == "TestUser"),
                                   It.Is <string>(chatId => chatId == "5")));
        }
예제 #3
0
        public void ThrowExceptionWhenUserNotFound()
        {
            //assert
            var options     = Utils.GetDbOptions("SaveUserConnectionShould_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.SaveUserConnection("ghost_user", "connectionId"));
            }
        }
예제 #4
0
        public void AddConnectionWhenUserParticipatesInLobby()
        {
            //assert
            var options     = Utils.GetDbOptions("SaveUserConnectionShould_AddConnectionWhenUserParticipatesInLobby");
            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
                });
                context.SaveChanges();
            }
            //act
            using (var context = new ApplicationDbContext(options))
            {
                var service = new LobbyService(context, null, chatService.Object);
                service.SaveUserConnection("test_user", "connectionId");
            }
            //assert
            using (var context = new ApplicationDbContext(options))
            {
                var connections = context.UserParticipationInLobbies.Find(new object[] { 5, "test_user" }).ConnectionIds;
                Assert.Equal(new HashSet <string> {
                    "connectionId"
                }, connections);
                chatService.Verify(cs => cs.AddConnectionToChat(
                                       It.Is <string>(connectionId => connectionId == "connectionId"),
                                       It.Is <string>(chatId => chatId == "5")));
            }
        }
예제 #5
0
        public void ThrowExceptionWhenUserDoesNotParticipateInAnyLobby()
        {
            //assert
            var options     = Utils.GetDbOptions("SaveUserConnectionShould_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.SaveUserConnection("test_user", "connectionId"));
            }
        }