public void Invoke_ValidData_ReturnsCorrectModel()
        {
            var guid          = Guid.NewGuid();
            var messageFromDb = new DataAccess.Models.Message
            {
                Id        = guid,
                Text      = "Testowa wiadomość",
                DeletedOn = null
            };

            // prepare
            var mockedMessageRepository = new Mock <IMessageRepository>();

            mockedMessageRepository.Setup(r => r.GetById(It.IsAny <Guid>()))
            .Returns(messageFromDb);
            var action = new GetMessageById(mockedMessageRepository.Object);

            // action
            var message = action.Invoke(guid);

            // check
            Assert.NotNull(message);
            Assert.Equal(guid, message.Id);
            Assert.Equal("Testowa wiadomość", message.Text);
            Assert.Null(message.DeletedOn);
            mockedMessageRepository.Verify(r => r.GetById(It.IsAny <Guid>()), Times.Once);
        }
Beispiel #2
0
        public void Invoke_ValidData_AddsMessageToDatabaseWithCorrectValues()
        {
            var messageData = new MessageModel
            {
                Text     = "Testmessage",
                UserRoom = new UserRoomModel()
            };

            DataAccess.Models.Message messageSaved = null;
            var mockedMessageRepository            = new Mock <IMessageRepository>();

            mockedMessageRepository.Setup(r => r.Add(It.IsAny <DataAccess.Models.Message>()))
            .Callback <DataAccess.Models.Message>(u => messageSaved = u);
            var mockedUnitOfWork = new Mock <IUnitOfWork>();

            var action = new AddNewMessage(mockedMessageRepository.Object, mockedUnitOfWork.Object);

            // action
            action.Invoke(messageData);

            // assert
            mockedMessageRepository.Verify(r => r.Add(It.IsAny <DataAccess.Models.Message>()), Times.Once());
            mockedUnitOfWork.Verify(r => r.Save(), Times.Exactly(1));
            Assert.Equal("Testmessage", messageSaved.Text);
        }
        public void Invoke_ValidData_ReturnsCorrectModel()
        {
            var guid          = Guid.NewGuid();
            var messageFromDb = new DataAccess.Models.Message
            {
                Id        = guid,
                Text      = "Testowa wiadomość",
                DeletedOn = null
            };

            // prepare
            DataAccess.Models.Message messageSaved = null;

            var mockedMessageRepository = new Mock <IMessageRepository>();

            mockedMessageRepository.Setup(r => r.GetById(It.IsAny <Guid>()))
            .Returns(messageFromDb);
            mockedMessageRepository.Setup(r => r.Edit(It.IsAny <DataAccess.Models.Message>()))
            .Callback <DataAccess.Models.Message>(u => messageSaved = u);
            var mockedUnitOfWork = new Mock <IUnitOfWork>();

            var action = new EditMessageById(mockedMessageRepository.Object, mockedUnitOfWork.Object);

            // action
            var message = action.Invoke(guid, "Nowa wiadomość");

            // check
            Assert.True(message);
            Assert.Equal(guid, messageSaved.Id);
            Assert.Equal("Nowa wiadomość", messageSaved.Text);
            Assert.Null(messageSaved.DeletedOn);
            mockedMessageRepository.Verify(r => r.GetById(It.IsAny <Guid>()), Times.Once);
            mockedUnitOfWork.Verify(r => r.Save(), Times.Once);
        }
        public void Invoke_ValidData_EditAndSaveAreCalled()
        {
            // prepare
            var user = new DataAccess.Models.User
            {
                Name = "TestUser"
            };

            var role = new DataAccess.Models.Role
            {
                Name = "TestRole"
            };

            var message = new DataAccess.Models.Message
            {
                Text = "TestMessage"
            };

            var room = new DataAccess.Models.Room();

            var userRoomInDb = new List <DataAccess.Models.UserRoom>
            {
                new DataAccess.Models.UserRoom
                {
                    Messages = new List <DataAccess.Models.Message> {
                        message
                    },
                    LastMessage = message,
                    Room        = room,
                    User        = user,
                    Role        = role,
                    LockedUntil = DateTime.MaxValue
                }
            };

            var mockedUserRoomRepository = new Mock <IUserRoomRepository>();

            mockedUserRoomRepository.Setup(r => r.FindBy(It.IsAny <Expression <Func <DataAccess.Models.UserRoom, bool> > >()))
            .Returns(userRoomInDb.AsQueryable());

            var action = new GetUserRoomByIds(mockedUserRoomRepository.Object);

            // action
            var result = action.Invoke(Guid.NewGuid(), Guid.NewGuid());

            // assert
            Assert.NotNull(result);
            Assert.Equal("TestMessage", result.Messages[0].Text);
            //Assert.Equal("TestUser", result.Room.Users[0].Name);
            Assert.Equal("TestUser", result.User.Name);
            Assert.Equal("TestRole", result.Role.Name);
            Assert.Equal(DateTime.MaxValue, result.LockedUntil);
            mockedUserRoomRepository.Verify(r => r.FindBy(It.IsAny <Expression <Func <DataAccess.Models.UserRoom, bool> > >()), Times.Once);
        }
        public void Invoke_ValidData_ReturnsCorrectModel()
        {
            // prepare
            var guidRoom = Guid.NewGuid();
            var guid     = Guid.NewGuid();

            var tag = new Tag
            {
                Name = "TestTag"
            };

            var room = new DataAccess.Models.Room
            {
                Name = "TestRoom"
            };

            var lastMessage = new DataAccess.Models.Message
            {
                Text = "LastMessage"
            };

            var userRoomsFromDb = new List <DataAccess.Models.UserRoom>
            {
                new DataAccess.Models.UserRoom {
                    Id          = guid,
                    Room        = room,
                    LastMessage = lastMessage,
                    Tags        = new List <Tag> {
                        tag
                    }
                }
            };

            var mockedUserRepository = new Mock <IUserRepository>();

            mockedUserRepository.Setup(r => r.GetPrivateConversations(It.IsAny <Guid>())).Returns(userRoomsFromDb.AsQueryable());
            var action = new GetUserPrivateConversations(mockedUserRepository.Object);

            // action
            var result = action.Invoke(guidRoom);

            // check
            Assert.NotNull(result);
            Assert.Equal(guid, result[0].Id);
            Assert.Equal("TestTag", result[0].Tags[0].Name);
            Assert.Equal("TestRoom", result[0].Room.Name);
            Assert.Equal("LastMessage", result[0].LastMessage.Text);
            mockedUserRepository.Verify(r => r.GetPrivateConversations(It.IsAny <Guid>()), Times.Once);
        }
Beispiel #6
0
        public void Invoke_ValidData_DeleteAndSaveAreCalled()
        {
            // prepare
            var guid          = Guid.NewGuid();
            var messageFromDb = new DataAccess.Models.Message
            {
                Id = guid
            };

            var mockedMessageRepository = new Mock <IMessageRepository>();

            mockedMessageRepository.Setup(r => r.GetById(guid)).Returns(messageFromDb);
            var mockedUnitOfWork = new Mock <IUnitOfWork>();

            var action = new DeleteMessageById(mockedMessageRepository.Object, mockedUnitOfWork.Object);

            // action
            var result = action.Invoke(guid);

            // assert
            Assert.True(result);
            mockedMessageRepository.Verify(r => r.Delete(It.IsAny <DataAccess.Models.Message>()), Times.Once());
            mockedUnitOfWork.Verify(r => r.Save(), Times.Once());
        }