Beispiel #1
0
        public void Edit_TopicNameIsNull_ThrowsArgumentNullException()
        {
            // Arrange
            int topicId = 1;

            ITopicService target = new TopicService(new Mock<IUnitOfWork>().Object, this._topicValidationMock.Object);

            // Act and Assert
            Assert.Throws<ArgumentNullException>(() => target.Edit(topicId, null));
        }
Beispiel #2
0
        public void Edit_TopicNameFormatIsInvalid_ThrowsInvalidTopicNameFormatException()
        {
            // Arrange
            int topicId = 1;
            string newTopicName = "invalid_new_topic_name";

            // Arrange - mock topicValidation
            this._topicValidationMock.Setup(v => v.IsValidName(newTopicName))
            .Returns(false);

            // Arrange - create target
            ITopicService target = new TopicService(new Mock<IUnitOfWork>().Object, this._topicValidationMock.Object);

            // Act and Assert
            Assert.Throws<InvalidTopicNameFormatException>(() => target.Edit(topicId, newTopicName));

            this._topicValidationMock.Verify(v => v.IsValidName(newTopicName), Times.Once);
        }
Beispiel #3
0
        public void Edit_NonexistentTopicId_ThrowsTopicNotFoundException()
        {
            // Arrange
            int topicId = 1;
            string newTopicName = "new_topic_name";

            // Arrange - mock topicRepository
            Mock<ITopicRepository> topicRepositoryMock = new Mock<ITopicRepository>();

            topicRepositoryMock.Setup(r => r.GetByName(newTopicName))
            .Returns((Topic)null);

            topicRepositoryMock.Setup(r => r.GetById(topicId))
            .Returns((Topic)null);

            // Arrange - mock unitOfWork
            Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>();

            unitOfWorkMock.SetupGet(u => u.TopicRepository)
            .Returns(topicRepositoryMock.Object);

            ITopicService target = new TopicService(unitOfWorkMock.Object, this._topicValidationMock.Object);

            // Act and Assert
            Assert.Throws<TopicNotFoundException>(() => target.Edit(topicId, newTopicName));

            topicRepositoryMock.Verify(r => r.GetByName(newTopicName), Times.Once);
            topicRepositoryMock.Verify(r => r.GetById(topicId), Times.Once);
            topicRepositoryMock.Verify(r => r.Update(It.Is<Topic>(t => t.TopicId == topicId)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
Beispiel #4
0
        public void Edit_TopicIdIsLessOrEqualToZero_ThrowsArgumentOutOfRangeException()
        {
            // Arrange
            string newTopicName = "new_topic_name";

            ITopicService target = new TopicService(new Mock<IUnitOfWork>().Object, this._topicValidationMock.Object);

            // Act and Assert
            Assert.Throws<ArgumentOutOfRangeException>(() => target.Edit(-1, newTopicName));
            Assert.Throws<ArgumentOutOfRangeException>(() => target.Edit(0, newTopicName));
        }
Beispiel #5
0
        public void Edit_AllCredentialsAreValid_EditsTopic()
        {
            // Arrange
            Topic topic = new Topic { TopicId = 1 };
            string newTopicName = "new_topic_name";

            // Arrange - mock topicRepository
            Mock<ITopicRepository> topicRepositoryMock = new Mock<ITopicRepository>();

            topicRepositoryMock.Setup(r => r.GetById(topic.TopicId))
            .Returns(topic);

            topicRepositoryMock.Setup(r => r.GetByName(newTopicName))
            .Returns((Topic)null);

            Topic editedTopic = null;

            topicRepositoryMock.Setup(r => r.Update(It.Is<Topic>(t => t.TopicId == topic.TopicId)))
            .Callback((Topic t) => editedTopic = t);

            // Arrange - mock unitOfWork
            Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>();

            unitOfWorkMock.SetupGet(u => u.TopicRepository)
            .Returns(topicRepositoryMock.Object);

            // Arrange - create target
            ITopicService target = new TopicService(unitOfWorkMock.Object, this._topicValidationMock.Object);

            // Act
            target.Edit(topic.TopicId, newTopicName);

            // Assert
            Assert.IsNotNull(editedTopic);
            Assert.AreEqual(newTopicName, editedTopic.Name);

            topicRepositoryMock.Verify(r => r.GetById(topic.TopicId), Times.Once);
            topicRepositoryMock.Verify(r => r.GetByName(newTopicName), Times.Once);
            topicRepositoryMock.Verify(r => r.Update(It.Is<Topic>(t => t.TopicId == topic.TopicId)), Times.Once);

            unitOfWorkMock.Verify(r => r.Save(), Times.Once);
        }