Ejemplo n.º 1
0
        public void Create_TagNameAlreadyExists_ThrowsTagNameAlreadyExistsException()
        {
            // Arrange
            string tagName = "tag_name";

            Tag tag = new Tag { TagId = 1, Name = tagName };

            // Arrange - mock tagRepository
            Mock<ITagRepository> tagRepositoryMock = new Mock<ITagRepository>();

            tagRepositoryMock.Setup(r => r.GetByName(tagName))
            .Returns(tag);

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

            unitOfWorkMock.SetupGet(u => u.TagRepository)
            .Returns(tagRepositoryMock.Object);

            // Arrange - create target
            ITagService target = new TagService(unitOfWorkMock.Object, this._tagValidationMock.Object);

            // Act and Assert
            Assert.Throws<TagNameAlreadyExistsException>(() => target.Create(tagName));

            tagRepositoryMock.Verify(r => r.GetByName(tagName), Times.Once);
            tagRepositoryMock.Verify(r => r.Insert(It.Is<Tag>(t => t.Name == tagName)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
Ejemplo n.º 2
0
        public Tag Create(string tagName)
        {
            Guard.NotNullOrEmpty(tagName, "tagName");

            if (!this._tagValidation.IsValidName(tagName))
            {
                throw new InvalidTagNameFormatException(String.Format("Tag's name '{0}' has invalid format.", tagName));
            }

            Tag tag = this._unitOfWork.TagRepository.GetByName(tagName);

            if (tag != null)
            {
                throw new TagNameAlreadyExistsException(String.Format("Tag with name '{0}' already exists.", tagName));
            }

            tag = new Tag { Name = tagName };

            this._unitOfWork.TagRepository.Insert(tag);

            this._unitOfWork.Save();

            return tag;
        }
Ejemplo n.º 3
0
        public void GetByName_TagNameIsValid_ReturnsTag()
        {
            // Arrange
            Tag testTag = new Tag { TagId = 1, Name = "tag_name" };

            // Arrange - mock tagRepository
            Mock<ITagRepository> tagRepositoryMock = new Mock<ITagRepository>();

            tagRepositoryMock.Setup(r => r.GetByName(testTag.Name))
            .Returns(testTag);

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

            unitOfWorkMock.SetupGet(u => u.TagRepository)
            .Returns(tagRepositoryMock.Object);

            // Arrange - create target
            ITagService target = new TagService(unitOfWorkMock.Object, this._tagValidationMock.Object);

            // Act
            Tag tag = target.GetByName(testTag.Name);

            // Assert
            Assert.AreSame(testTag, tag);
        }
Ejemplo n.º 4
0
        public void Edit_AllCredentialsAreValid_EditsTag()
        {
            // Arrange
            Tag tag = new Tag { TagId = 1 };
            string newTagName = "new_tag_name";

            // Arrange - mock tagRepository
            Mock<ITagRepository> tagRepositoryMock = new Mock<ITagRepository>();

            tagRepositoryMock.Setup(r => r.GetById(tag.TagId))
            .Returns(tag);

            tagRepositoryMock.Setup(r => r.GetByName(newTagName))
            .Returns((Tag)null);

            Tag editedTag = null;

            tagRepositoryMock.Setup(r => r.Update(It.Is<Tag>(t => t.TagId == tag.TagId)))
            .Callback((Tag t) => editedTag = t);

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

            unitOfWorkMock.SetupGet(u => u.TagRepository)
            .Returns(tagRepositoryMock.Object);

            // Arrange - create target
            ITagService target = new TagService(unitOfWorkMock.Object, this._tagValidationMock.Object);

            // Act
            target.Edit(tag.TagId, newTagName);

            // Assert
            Assert.IsNotNull(editedTag);
            Assert.AreEqual(newTagName, editedTag.Name);

            tagRepositoryMock.Verify(r => r.GetById(tag.TagId), Times.Once);
            tagRepositoryMock.Verify(r => r.GetByName(newTagName), Times.Once);
            tagRepositoryMock.Verify(r => r.Update(It.Is<Tag>(t => t.TagId == tag.TagId)), Times.Once);

            unitOfWorkMock.Verify(r => r.Save(), Times.Once);
        }
Ejemplo n.º 5
0
        public void Delete_TagIdIsValid_DeletesTag()
        {
            // Arrange
            Tag tag = new Tag { TagId = 1 };

            // Arrange - mock tagRepository
            Mock<ITagRepository> tagRepositoryMock = new Mock<ITagRepository>();

            tagRepositoryMock.Setup(r => r.GetById(tag.TagId))
            .Returns(tag);

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

            unitOfWorkMock.SetupGet(u => u.TagRepository)
            .Returns(tagRepositoryMock.Object);

            ITagService target = new TagService(unitOfWorkMock.Object, this._tagValidationMock.Object);

            // Act
            target.Delete(tag.TagId);

            // Assert
            tagRepositoryMock.Verify(r => r.GetById(tag.TagId), Times.Once);
            tagRepositoryMock.Verify(r => r.Delete(It.Is<Tag>(t => t.TagId == tag.TagId)), Times.Once);

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