コード例 #1
0
        public void RegisterIpAddress_IpAddressIsAlreadyRegistered_GetsVisitorByIpAddress()
        {
            // Arrange
            Visitor visitor = new Visitor { VisitorId = 1, IpAddress = "127.0.0.1" };

            // Arrange - mock visitorRepository
            Mock<IVisitorRepository> visitorRepositoryMock = new Mock<IVisitorRepository>();

            visitorRepositoryMock.Setup(r => r.GetByIpAddress(visitor.IpAddress))
            .Returns(visitor);

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

            unitOfWorkMock.SetupGet(unitOfWork => unitOfWork.VisitorRepository)
            .Returns(visitorRepositoryMock.Object);

            // Arrange - create target
            IVisitorService target = new VisitorService(unitOfWorkMock.Object, this._visitorValidationMock.Object);

            // Act
            Visitor registeredVisitor = target.RegisterIpAddress(visitor.IpAddress);

            // Assert
            Assert.AreSame(visitor, registeredVisitor);

            visitorRepositoryMock.Verify(r => r.GetByIpAddress(visitor.IpAddress), Times.Once);
            visitorRepositoryMock.Verify(r => r.Insert(It.Is<Visitor>(v => v.VisitorId == visitor.VisitorId)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
コード例 #2
0
        public void RegisterIpAddress_IpAddressFormatIsInvalid_ThrowsInvalidIpAddressFormatException()
        {
            // Arrange
            string ipAddress = "127.0.0.1";

            this._visitorValidationMock.Setup(v => v.IsValidIpAddress(ipAddress))
            .Returns(false);

            IVisitorService target = new VisitorService(new Mock<IUnitOfWork>().Object, this._visitorValidationMock.Object);

            // Act and Assert
            Assert.Throws<InvalidIpAddressFormatException>(() => target.RegisterIpAddress(ipAddress));

            this._visitorValidationMock.Verify(v => v.IsValidIpAddress(ipAddress), Times.Once);
        }
コード例 #3
0
        public void RegisterIpAddress_IpAddressIsNotRegistered_RegistersIpAddressAndGetsVisitorByIpAddress()
        {
            // Arrange
            string ipAddress = "127.0.0.1";

            // Arrange - mock visitorRepository
            Mock<IVisitorRepository> visitorRepositoryMock = new Mock<IVisitorRepository>();

            visitorRepositoryMock.Setup(r => r.GetByIpAddress(ipAddress))
            .Returns((Visitor)null);

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

            unitOfWorkMock.SetupGet(u => u.VisitorRepository)
            .Returns(visitorRepositoryMock.Object);

            // Arrange - create target
            IVisitorService target = new VisitorService(unitOfWorkMock.Object, this._visitorValidationMock.Object);

            // Act
            Visitor registeredVisitor = target.RegisterIpAddress(ipAddress);

            // Assert
            Assert.IsNotNull(registeredVisitor);
            Assert.AreEqual(ipAddress, registeredVisitor.IpAddress);

            visitorRepositoryMock.Verify(r => r.GetByIpAddress(ipAddress), Times.Once);
            visitorRepositoryMock.Verify(r => r.Insert(It.Is<Visitor>(v => v.IpAddress == ipAddress)), Times.Once);

            unitOfWorkMock.Verify(r => r.Save(), Times.Once);
        }
コード例 #4
0
        public void RegisterIpAddress_IpAddressIsNull_ThrowsArgumentNullException()
        {
            // Arrange
            IVisitorService target = new VisitorService(new Mock<IUnitOfWork>().Object, this._visitorValidationMock.Object);

            // Act and Assert
            Assert.Throws<ArgumentNullException>(() => target.RegisterIpAddress(null));
        }