Exemplo n.º 1
0
        public void InviteToChat()
        {
            var guid1 = Guid.NewGuid();
            var guid2 = Guid.NewGuid();

            ChatHub.ConnectedUsers.Add(new UserModel()
            {
                Id = guid1,
                Language = "English",
                UserName = "******",
                IsChatting = false,
                Invites = new List<Guid>(),
                ConnectionIds = new Dictionary<string, List<string>>() { { "Lobby", new List<string>() { "C5" } } }
            });
            ChatHub.ConnectedUsers.Add(new UserModel()
            {
                Id = guid2,
                Language = "French",
                UserName = "******",
                IsChatting = false,
                Invites = new List<Guid>(),
                ConnectionIds = new Dictionary<string, List<string>>() { { "Lobby", new List<string>() { "C6" } } }
            });

            UserModel inviter = null;

            var hub = new ChatHub(new MockTranslator());
            var mockClients = new Mock<IHubCallerConnectionContext<dynamic>>();
            hub.Clients = mockClients.Object;
            dynamic all = new ExpandoObject();
            all.inviteReceived = new Action<UserModel>((name) =>
            {
                inviter = name;
            });

            AttachIdentity(hub, "Tester5", "C5");

            mockClients.Setup(m => m.All).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Caller).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Others).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.OthersInGroup(It.IsAny<string>())).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.User(It.IsAny<string>())).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Client(It.IsAny<string>())).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Clients(It.IsAny<List<string>>())).Returns((ExpandoObject)all);

            hub.InviteUser(guid1, guid2);

            Assert.AreEqual(guid2, ChatHub.ConnectedUsers.First(cu => cu.UserName == "Tester5").Invites[0]);
            Assert.IsNotNull(inviter);
            Assert.AreEqual(guid1, inviter.Id);
        }