示例#1
0
        public void Create_TopicNameAlreadyExists_ThrowsTopicNameAlreadyExistsException()
        {
            // Arrange
            string topicName = "topic_name";

            Topic topic = new Topic { TopicId = 1, Name = topicName };

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

            topicRepositoryMock.Setup(r => r.GetByName(topicName))
            .Returns(topic);

            // 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 and Assert
            Assert.Throws<TopicNameAlreadyExistsException>(() => target.Create(topicName));

            topicRepositoryMock.Verify(r => r.GetByName(topicName), Times.Once);
            topicRepositoryMock.Verify(r => r.Insert(It.Is<Topic>(t => t.Name == topicName)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
示例#2
0
        public void Create_TopicNameIsEmpty_ThrowsArgumentException()
        {
            // Arrange
            ITopicService target = new TopicService(new Mock<IUnitOfWork>().Object, this._topicValidationMock.Object);

            // Act and Assert
            Assert.Throws<ArgumentException>(() => target.Create(""));
        }
示例#3
0
        public void Create_TopicNameFormatIsInvalid_ThrowsInvalidTopicNameFormatException()
        {
            // Arrange
            string topicName = "invalid_topic_name";

            this._topicValidationMock.Setup(v => v.IsValidName(topicName))
            .Returns(false);

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

            // Act and Assert
            Assert.Throws<InvalidTopicNameFormatException>(() => target.Create(topicName));

            this._topicValidationMock.Verify(v => v.IsValidName(topicName), Times.Once);
        }
示例#4
0
        public void Create_TopicNameIsValid_CreatesTopic()
        {
            // Arrange
            string topicName = "topicName";

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

            // 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
            Topic createdTopic = target.Create(topicName);

            // Assert
            Assert.IsNotNull(createdTopic);
            Assert.AreEqual(topicName, createdTopic.Name);

            topicRepositoryMock.Verify(r => r.Insert(It.Is<Topic>(t => t.Name == topicName)), Times.Once);

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