コード例 #1
0
        public void RepositoryGetById()
        {
            //arrange
            var connection = DbConnectionFactory.CreateTransient();
            var stubDB     = new GroceryStoreContext(connection);
            var userRepo   = new EfGenericRepository <User>(stubDB);

            var userToAdd = new User
            {
                FirstName   = "Pesho",
                LastName    = "Pesho",
                Username    = "******",
                Password    = "******",
                PhoneNumber = "Pesho"
            };

            userRepo.Add(userToAdd);
            stubDB.SaveChanges();

            //act
            var actual = userRepo.GetById(1);

            //assert
            Assert.AreEqual(userToAdd.Username, actual.Username);
        }
コード例 #2
0
        public void RepositoryUpdate()
        {
            //arrange
            var connection = DbConnectionFactory.CreateTransient();
            var stubDB     = new GroceryStoreContext(connection);
            var userRepo   = new EfGenericRepository <User>(stubDB);

            var user = new User
            {
                FirstName   = "Pesho",
                LastName    = "Pesho",
                Username    = "******",
                Password    = "******",
                PhoneNumber = "Pesho"
            };

            userRepo.Add(user);
            stubDB.SaveChanges();

            //act
            user.Password = "******";
            userRepo.Update(user);

            string actual = userRepo.GetById(1).Password;

            //assert
            Assert.AreEqual("newPassword", actual);
        }
コード例 #3
0
        public void ThrowArgumentNullExceptionWhenPassedEntityIsNull()
        {
            //Arrange
            var pages = new List <Page>
            {
                new Page()
                {
                    Id = Guid.NewGuid(), Title = "page", Content = "cntnt"
                },
                new Page()
                {
                    Id = Guid.NewGuid(), Title = "page", Content = "cntnt"
                }
            };
            var mockedDbContext = new Mock <ISotnWikiDbContext>();
            var mockedPageSet   = QueryableDbSetMock.GetQueryableMockDbSet <Page>(pages);

            mockedDbContext.Setup(c => c.Set <Page>()).Returns(mockedPageSet);
            mockedDbContext.Setup(c => c.Pages).Returns(mockedPageSet);
            string expectedExceptionMessage = "id";

            var repositoryUnderTest = new EfGenericRepository <Page>(mockedDbContext.Object);

            //Act & Assert
            var exc = Assert.Throws <ArgumentNullException>(() => repositoryUnderTest.GetById(null));

            //Assert
            StringAssert.Contains(expectedExceptionMessage, exc.Message);
        }
コード例 #4
0
        public void ReturnCorrectResult_WhenParameterIsValid(int validId)
        {
            // Arrange
            var mockedDbSet     = new Mock <DbSet <IAdvert> >();
            var mockedDbContext = new Mock <ICarAdvertsSystemDbContext>();

            var fakeAdvert = new Mock <IAdvert>();

            fakeAdvert.SetupSet(f => f.Id = validId);

            mockedDbContext.Setup(mock => mock.Set <IAdvert>()).Returns(mockedDbSet.Object);
            var repository = new EfGenericRepository <IAdvert>(mockedDbContext.Object);

            mockedDbSet.Setup(x => x.Find(It.IsAny <int>())).Returns(fakeAdvert.Object);

            // Act and Assert
            Assert.That(repository.GetById(validId), Is.Not.Null);
            Assert.AreEqual(repository.GetById(validId), fakeAdvert.Object);
        }
コード例 #5
0
        public void InvockeDbSet_MethodFindOnce(int?id)
        {
            // Arrange
            var mockedDbSet     = new Mock <DbSet <IAdvert> >();
            var mockedDbContext = new Mock <ICarAdvertsSystemDbContext>();

            mockedDbContext.Setup(mock => mock.Set <IAdvert>()).Returns(mockedDbSet.Object);
            var repository = new EfGenericRepository <IAdvert>(mockedDbContext.Object);

            repository.GetById(id);

            // Assert
            mockedDbSet.Verify(x => x.Find(It.IsAny <int?>()), Times.Once);
        }
コード例 #6
0
        public void ReturnNull_WhenRequestedIdIsNull()
        {
            // Arrange
            var dbContext = new Mock <ICarAdvertsSystemDbContext>();

            var entities = new Mock <DbSet <MockDbModel> >();

            dbContext.Setup(c => c.Set <MockDbModel>()).Returns(entities.Object);

            var efGenericRepository = new EfGenericRepository <MockDbModel>(dbContext.Object);

            var fakeData = new HashSet <MockDbModel>()
            {
                new MockDbModel()
                {
                    Id = 1
                },
                new MockDbModel()
                {
                    Id = 2
                },
                new MockDbModel()
                {
                    Id = 3
                },
                new MockDbModel()
                {
                    Id = 4
                },
                new MockDbModel()
                {
                    Id = 5
                }
            };

            var fakeDataQueryable = fakeData.AsQueryable();

            entities.As <IQueryable>().Setup(e => e.GetEnumerator()).Returns(fakeDataQueryable.GetEnumerator());
            entities.As <IQueryable>().Setup(e => e.ElementType).Returns(fakeDataQueryable.ElementType);
            entities.As <IQueryable>().Setup(e => e.Expression).Returns(fakeDataQueryable.Expression);
            entities.As <IQueryable>().Setup(e => e.Provider).Returns(fakeDataQueryable.Provider);

            // Act
            var actualReturnedTEntity = efGenericRepository.GetById(null);

            // Assert
            Assert.That(actualReturnedTEntity, Is.Null);
        }
        public void EfGenericRepository_ShouldGetSpecificUserById()
        {
            var user = new User()
            {
                Id         = "test",
                FirstName  = "Gosho",
                SecondName = "Petrov",
                LastName   = "Ivanov"
            };
            var mockedDb = new Mock <ICarsSystemDbContext>();

            mockedDb.Setup(m => m.Set <User>().Find("test")).Returns(user);
            var repository = new EfGenericRepository <User>(mockedDb.Object);

            repository.GetById("test");

            mockedDb.Verify(m => m.Set <User>().Find("test"), Times.Exactly(1));
        }
コード例 #8
0
        public void ReturnExpectedResult()
        {
            // Arrange
            var mockedDbContext = new Mock <IDbContext>();
            var mockedSet       = new Mock <DbSet <Dummy> >();

            mockedDbContext.Setup(c => c.Set <Dummy>()).Returns(mockedSet.Object);
            var efRepository = new EfGenericRepository <Dummy>(mockedDbContext.Object);

            var expectedDummy = new Dummy()
            {
                Id = It.IsAny <int>(), Name = It.IsAny <string>()
            };

            mockedSet.Setup(s => s.Find(It.IsAny <int>())).Returns(expectedDummy);

            // Act
            var actualDummy = efRepository.GetById(It.IsAny <int>());

            // Assert
            Assert.AreEqual(expectedDummy, actualDummy);
        }