Exemple #1
0
        public void CanInsert()
        {
            var flyRepository = new Mock<IFlyRepository>();
            var flyService = new FlyService(flyRepository.Object);

            var fly = new Fly { DateOfFly = DateTime.Now, Description = "First fly", Owner = new FlyOwner { Id = 1, Name = "Owner 1" } };

            flyService.Insert(fly);

            flyRepository.Verify(v => v.Insert(It.IsAny<Fly>()), Times.Once());
        }
Exemple #2
0
        public void CanGetById()
        {
            var flyRepository = new Mock<IFlyRepository>();

            flyRepository.Setup(s => s.GetById(It.IsAny<int>())).Returns(
                    new Fly { Id = 1, DateOfFly = DateTime.Now });

            var flyService = new FlyService(flyRepository.Object);

            Assert.AreEqual(flyService.GetById(1).Id, 1);
            flyRepository.Verify(v => v.GetById(It.IsAny<int>()), Times.Once());
        }
Exemple #3
0
        public void CanDelete()
        {
            var flyRepository = new Mock<IFlyRepository>();

            flyRepository.Setup(s => s.GetById(It.IsAny<int>())).Returns(
                new Fly { Id = 1, DateOfFly = DateTime.Now, Description = "First Fly", Owner = new FlyOwner { Id = 1, Name = "Owner 1" } });

            var flyService = new FlyService(flyRepository.Object);

            flyService.Delete(1);

            flyRepository.Verify(v => v.Delete(It.IsAny<int>()), Times.Once());
        }
Exemple #4
0
        public void CanNotInsertWithNullFly()
        {
            var flyRepository = new Mock<IFlyRepository>();
            var flyService = new FlyService(flyRepository.Object);

            try
            {
                flyService.Insert(null);
                Assert.Fail("Validation not implemented!");
            }
            catch (ArgumentException argex)
            {
                Assert.AreEqual(argex.Message, "Fly is required!");
                flyRepository.Verify(v => v.Insert(It.IsAny<Fly>()), Times.Never());
            }
        }
Exemple #5
0
        public void CanGetAll()
        {
            var flyRepository = new Mock<IFlyRepository>();

            flyRepository.Setup(s => s.GetAll()).Returns(new List<Fly>
                {
                    new Fly { Id = 1, DateOfFly = DateTime.Now },
                    new Fly { Id = 2, DateOfFly = DateTime.Now }
                }.AsQueryable());

            var flyService = new FlyService(flyRepository.Object);

            var flys = flyService.GetAll();

            Assert.AreEqual(flys.Count(), 2);
            Assert.AreEqual(flys.First().Id, 1);
            flyRepository.Verify(v => v.GetAll(), Times.Once());
        }
Exemple #6
0
        public void CanNotUpdateWithoutOwner()
        {
            var flyRepository = new Mock<IFlyRepository>();
            var flyService = new FlyService(flyRepository.Object);

            try
            {
                flyService.Update(new Fly { Id = 1 });
                Assert.Fail("Validation not implemented!");
            }
            catch (ArgumentException argex)
            {
                Assert.AreEqual("Fly not exists!", argex.Message);
                flyRepository.Verify(v => v.Update(It.IsAny<Fly>()), Times.Never());
            }

            flyRepository.Verify(v => v.GetById(It.IsAny<int>()), Times.Once());
        }
Exemple #7
0
        public void CanNotUpdateWithoutDescription()
        {
            var flyRepository = new Mock<IFlyRepository>();

            flyRepository.Setup(s => s.GetById(1)).Returns(new Fly { Id = 1, DateOfFly = DateTime.Now, Description="First Fly", Owner = new FlyOwner { Id = 1, Name = "Owner 1" } });

            var flyService = new FlyService(flyRepository.Object);

            try
            {
                var fly = flyService.GetById(1);

                fly.Description = string.Empty;

                flyService.Update(fly);

                Assert.Fail("Validation not implemented!");
            }
            catch (ArgumentException argex)
            {
                Assert.AreEqual("Description is required!", argex.Message);
                flyRepository.Verify(v => v.Update(It.IsAny<Fly>()), Times.Never());
            }

            flyRepository.Verify(v => v.GetById(It.IsAny<int>()), Times.AtLeastOnce());
        }
Exemple #8
0
        public void CanNotUpdateAbsentFly()
        {
            var flyRepository = new Mock<IFlyRepository>();

            flyRepository.Setup(s => s.GetById(1)).Returns(
             new Fly { Id = 1, DateOfFly = DateTime.Now, Owner = new FlyOwner { Id = 1, Name = "Owner 1" } });

            var flyService = new FlyService(flyRepository.Object);

            try
            {
                flyService.Update(new Fly { Id = 0 });
                Assert.Fail("Validation not implemented!");
            }
            catch (ArgumentException argex)
            {
                Assert.AreEqual("Fly not exists!", argex.Message);
                flyRepository.Verify(v => v.Update(It.IsAny<Fly>()), Times.Never());
            }

            flyRepository.Verify(v => v.GetById(It.IsAny<int>()), Times.Once());
        }
Exemple #9
0
        public void CanNotInsertWithoutOwner()
        {
            var flyRepository = new Mock<IFlyRepository>();
            var flyService = new FlyService(flyRepository.Object);

            var fly = new Fly { DateOfFly = DateTime.Now, Description = "First fly" };

            try
            {
                flyService.Insert(fly);
                Assert.Fail("Validation not implemented!");
            }
            catch (ArgumentException argex)
            {
                Assert.AreEqual("Owner is required!", argex.Message);
                flyRepository.Verify(v => v.Insert(It.IsAny<Fly>()), Times.Never());
            }
        }