示例#1
0
        public async Task GetByIdAsync_UnknownIdAndWithTracking_ShouldReturnNull()
        {
            // Arrange
            TestEntity[] entities = _fixture.CreateMany <TestEntity>(2).ToArray();

            var dbContextMock = new DbContextMock <TestDbContext>(_options);

            dbContextMock.CreateDbSetMock(x => x.TestEntities, (x, _) => x.Id, entities);
            var repository = new ReadBaseRepository <TestEntity, Guid, TestDbContext>(dbContextMock.Object);

            // Act
            TestEntity result = await repository.GetById(Guid.NewGuid(), true);

            // Assert
            result.Should().BeNull();
        }
示例#2
0
        public async Task GetByIdAsync_ShouldReturnEntity()
        {
            // Arrange
            TestEntity[] entities = _fixture.CreateMany <TestEntity>(2).ToArray();

            var dbContextMock = new DbContextMock <TestDbContext>(_options);

            dbContextMock.CreateDbSetMock(x => x.TestEntities, (x, _) => x.Id, entities);

            var repository = new ReadBaseRepository <TestEntity, Guid, TestDbContext>(dbContextMock.Object);

            // Act
            TestEntity result = await repository.GetById(entities[0].Id);

            // Assert
            result.Should().NotBeNull();
            result.Should().BeEquivalentTo(entities[0]);
        }
示例#3
0
        public async Task ListAllAsync_WithTracking_ShouldReturnEntities()
        {
            // Arrange
            TestEntity[] entities = _fixture.CreateMany <TestEntity>(2).ToArray();

            var dbContextMock = new DbContextMock <TestDbContext>(_options);

            dbContextMock.CreateDbSetMock(x => x.TestEntities, (x, _) => x.Id, entities);
            var repository = new ReadBaseRepository <TestEntity, Guid, TestDbContext>(dbContextMock.Object);

            // Act
            TestEntity[] result = await repository.GetAll(true);

            // Assert
            result.Should()
            .NotBeNull().And
            .HaveCount(2);
        }
示例#4
0
        public async Task ListAllAsyncWithSpecification_ShouldApplySpecification()
        {
            // Arrange
            TestEntity[] entities = _fixture.CreateMany <TestEntity>(2).ToArray();

            var dbContextMock = new DbContextMock <TestDbContext>(_options);

            dbContextMock.CreateDbSetMock(x => x.TestEntities, (x, _) => x.Id, entities);
            dbContextMock.Setup(s => s.Set <TestEntity>().AsQueryable()).Returns(entities.AsQueryable());
            var repository = new ReadBaseRepository <TestEntity, Guid, TestDbContext>(dbContextMock.Object);

            var specification = new Mock <ISpecification <TestEntity> >();

            // Act
            TestEntity[] result = await repository.GetAll(specification.Object);

            // Assert
            result.Should()
            .NotBeEmpty().And
            .BeOfType(typeof(TestEntity[])).And
            .BeEquivalentTo(entities);
        }