コード例 #1
0
        public void GetById_Should_NotBeCalled_IfNotCalledYolo()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            mockedRepository.Verify(rep => rep.GetById(1), Times.Never);
        }
コード例 #2
0
        public void Constructor_Should_CreateAdvertServices_IfParamsAreValid()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            Assert.That(advertService, Is.InstanceOf <AdvertServices>());
        }
コード例 #3
0
        public void GetAllAdverts_Should_NotBeCalled_IfItIsNeverCalled()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            mockedRepository.Verify(rep => rep.All(), Times.Never);
        }
コード例 #4
0
        public void DeleteAdvert_Should_NotCallSaveChanges_IfAdvertIsNotDelete()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            var advertThatIsNotAdded = new Mock <Advert>();

            mockedUnitOfWork.Verify(u => u.SaveChanges(), Times.Never);
        }
コード例 #5
0
        public void GetAllAdverts_Should_BeCalled_IfParamsAreValid()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            advertService.GetAllAdverts();

            mockedRepository.Verify(rep => rep.All(), Times.Once);
        }
コード例 #6
0
        public void DeleteAdvert_Should_NotDeleteAdvertThatIsNeverAddedInTheFirstPlace()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            var advertThatIsNotAdded = new Mock <Advert>();

            mockedRepository.Verify(rep => rep.Delete(advertThatIsNotAdded.Object), Times.Never);
        }
コード例 #7
0
        public void GetById_ShouldThrowNullReferenceException_IfAdvertIsNull()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            Mock <Advert> advertThatIsNull = null;

            Assert.Throws <NullReferenceException>(() => advertService.GetById(advertThatIsNull.Object.Id));
        }
コード例 #8
0
        public void GetById_Should_NotReturnAdvert_IfThereIsNoAdvertYolo()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            mockedRepository.Setup(rep => rep.GetById(0)).Returns(() => null);

            Assert.IsNull(advertService.GetById(0));
        }
コード例 #9
0
        public void AddAdvert_ShouldThrowNullReferenceException_IfPassedAdvertIsNull()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            Mock <Advert> advertToAdd = null;

            Assert.Throws <NullReferenceException>(() => advertService.AddAdvert(advertToAdd.Object));
        }
コード例 #10
0
        public void Count_Should_ReturnZero_IfThereAreNoAdverts()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            IEnumerable <Advert> expectedResult = new List <Advert>();

            mockedRepository.Setup(rep => rep.All()).Returns(() => expectedResult.AsQueryable());

            Assert.AreEqual(0, advertService.Count());
        }
コード例 #11
0
        public void GetById_Should_ReturnTheProperAdvertWithId_IfCalled()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            var advertWithId = new Mock <Advert>();

            mockedRepository.Setup(rep => rep.GetById(advertWithId.Object.Id)).Returns(() => advertWithId.Object);

            Assert.IsInstanceOf <Advert>(advertService.GetById(advertWithId.Object.Id));
        }
コード例 #12
0
        public void AddAdvert_Should_CallSaveChanges_IfAdvertIsValid()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            var advertToAdd = new Mock <Advert>();

            advertService.AddAdvert(advertToAdd.Object);

            mockedUnitOfWork.Verify(u => u.SaveChanges(), Times.Once);
        }
コード例 #13
0
        public void AddAdvert_Should_BeCalledOnce_IfAdvertIsValid()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            var advertToAdd = new Mock <Advert>();

            advertService.AddAdvert(advertToAdd.Object);

            mockedRepository.Verify(rep => rep.Add(It.IsAny <Advert>()), Times.Once);
        }
コード例 #14
0
        public void Count_Should_NeverBeCalled_IfNotCalled()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            var advertToAdd = new Mock <Advert>();

            advertService.AddAdvert(advertToAdd.Object);

            mockedRepository.Verify(rep => rep.All(), Times.Never);
        }
コード例 #15
0
        public void GetAllAdverts_Should_ThrowArgumentNullException_IfPassedAdvertsAreNull()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            IEnumerable <Advert> expectedAdvertsResult = null;

            mockedRepository.Setup(rep => rep.All()).Returns(() => expectedAdvertsResult.AsQueryable());

            Assert.Throws <ArgumentNullException>(() => advertService.GetAllAdverts());
        }
コード例 #16
0
        public void GetById_Should_BeCalledOnce_IfParamsAreValid()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            var advert = new Mock <Advert>();

            advertService.GetById(advert.Object.Id);

            mockedRepository.Verify(rep => rep.GetById(advert.Object.Id), Times.Once);
        }
コード例 #17
0
        public void GetAllAdverts_Should_ReturnEmptyCollection_IfThereAreNoAdvertsAdded()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            IEnumerable <Advert> expectedAdvertsResult = new List <Advert>();

            mockedRepository.Setup(rep => rep.All()).Returns(() => expectedAdvertsResult.AsQueryable());

            Assert.IsEmpty(advertService.GetAllAdverts());
        }
コード例 #18
0
        public void GetById_Should_Work_IfCalledWithValidParams()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            var advertWithId = new Mock <Advert>();

            mockedRepository.Setup(rep => rep.GetById(advertWithId.Object.Id)).Returns(() => advertWithId.Object);

            Assert.AreEqual(advertService.GetById(advertWithId.Object.Id), advertWithId.Object);
        }
コード例 #19
0
        public void DeleteAdvert_Should_BeCalled_IfAdvertIsValid()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            var advertToAddAndDelete = new Mock <Advert>();

            advertService.AddAdvert(advertToAddAndDelete.Object);
            advertService.DeleteAdvert(advertToAddAndDelete.Object);

            mockedRepository.Verify(rep => rep.Delete(advertToAddAndDelete.Object));
        }
コード例 #20
0
        public void DeleteAdvert_Should_CallSaveChangesTwoTimes_IfAdvertIsValid()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            var advert = new Mock <Advert>();

            advertService.AddAdvert(advert.Object);
            advertService.DeleteAdvert(advert.Object);

            mockedUnitOfWork.Verify(u => u.SaveChanges(), Times.Exactly(2));
        }
コード例 #21
0
        public void DeleteAdvertById_Should_CallSaveChanges_IfParamsAreValid()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            var advertWithId = new Mock <Advert>();

            advertService.AddAdvert(advertWithId.Object);
            advertService.DeleteAdvertById(advertWithId.Object.Id);

            mockedUnitOfWork.Verify(u => u.SaveChanges(), Times.Exactly(2));
        }
コード例 #22
0
        public void GetById_Should_ReturnTheCorrectAdvert_IfCalled()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            var advert       = new Mock <Advert>();
            var secondAdvert = new Mock <Advert>();

            mockedRepository.Setup(rep => rep.GetById(advert.Object.Id)).Returns(() => advert.Object);

            Assert.AreNotEqual(advertService.GetById(advert.Object.Id), secondAdvert.Object);
        }
コード例 #23
0
        public void DeleteAdvertById_Should_BeCalledOnce_IfAdvertToDeleteIsValid()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            var advertToDeleteWithId = new Mock <Advert>();

            advertService.AddAdvert(advertToDeleteWithId.Object);
            advertService.DeleteAdvertById(advertToDeleteWithId.Object.Id);

            mockedRepository.Verify(rep => rep.Delete(advertToDeleteWithId.Object.Id), Times.Once);
        }
コード例 #24
0
        public void Count_Should_ReturnExactNumberOfAdverts_IfParamsAreValid()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            IEnumerable <Advert> expectedResult = new List <Advert>()
            {
                new Advert(), new Advert()
            };

            mockedRepository.Setup(rep => rep.All()).Returns(() => expectedResult.AsQueryable());

            Assert.AreEqual(2, advertService.Count());
        }
コード例 #25
0
        public void GetAllAdverts_Should_DoItsJobCorrectly_IfCalled()
        {
            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            IEnumerable <Advert> expectedAdvertsResult = new List <Advert>()
            {
                new Advert(), new Advert()
            };

            mockedRepository.Setup(rep => rep.All()).Returns(() => expectedAdvertsResult.AsQueryable());

            Assert.AreEqual(advertService.GetAllAdverts(), expectedAdvertsResult);
        }
コード例 #26
0
        public void ReturnCorrectAdverts_WhenCorrectParameters()
        {
            int vehicleModelId = 1;
            int cityId         = 1;
            int minPrice       = 1;
            int maxPrice       = 4;
            int yearFrom       = 1958;
            int yearTo         = 3000;

            var mockedRepository = new Mock <IRepository <Advert> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var advertService    = new AdvertServices(mockedRepository.Object, mockedUnitOfWork.Object);

            var advert       = new Mock <Advert>();
            var secondAdvert = new Mock <Advert>();

            mockedRepository.Setup(rep => rep.All()).Returns(() => new List <Advert>()
            {
                new Advert {
                    Id = 1, VehicleModelId = 1, CityId = 1, Price = 2, Year = 2000
                },
                new Advert {
                    Id = 2, VehicleModelId = 1, CityId = 1, Price = 2, Year = 2000
                },
                new Advert {
                    Id = 3, VehicleModelId = 1, CityId = 1, Price = 2, Year = 2000
                },
                new Advert {
                    Id = 4, VehicleModelId = 2, CityId = 1, Price = 2, Year = 2000
                }
            }.AsQueryable());

            // Act
            var result = advertService.GetAdvertsByMultipleParameters(vehicleModelId, cityId, minPrice, maxPrice, yearFrom, yearTo).ToList();

            Assert.AreEqual(3, result.Count);
        }