public async Task CharactersOfTypeHumanSpecification_ShouldApplySpecification() { // Arrange var baseCharacters = _fixture.Build <Character>() .Without(wh => wh.CharacterEpisodes) .Without(wh => wh.CharacterFriends) .Without(wh => wh.FriendCharacters).CreateMany(2); var humanCharacters = _fixture.Build <Human>() .Without(wh => wh.CharacterEpisodes) .Without(wh => wh.CharacterFriends) .Without(wh => wh.FriendCharacters) .Without(wh => wh.HomePlanet).CreateMany(2); List <Character> characters = new List <Character>(); characters.AddRange(baseCharacters); characters.AddRange(humanCharacters); Character[] entities = characters.ToArray(); var dbContextMock = new DbContextMock <TestDbContext>(_options); var set = dbContextMock.CreateDbSetMock(x => x.Characters, (x, _) => (x.Id), entities); var repository = new ReadAsyncRepository <Character>(dbContextMock.Object); // Act Character[] charactersResult = await repository.GetAllAsync(new CharactersOfTypeHumanSpecification()); // Assert charactersResult.Should().NotBeNull().And.HaveSameCount(humanCharacters); }
public async Task WithFriendsFromTatooineSpecification_ShouldApplySpecification() { // Arrange var tatooineHomeplant = _fixture.Build <Planet>() .Without(wh => wh.Humans).Create(); tatooineHomeplant.Name = "Tatooine"; var baseCharacter = _fixture.Build <Character>() .Without(wh => wh.CharacterEpisodes) .Without(wh => wh.CharacterFriends) .Without(wh => wh.FriendCharacters).Create(); var friendCharacter = _fixture.Build <Human>() .Without(wh => wh.CharacterEpisodes) .Without(wh => wh.CharacterFriends) .Without(wh => wh.FriendCharacters) .Without(wh => wh.HomePlanet).Create(); friendCharacter.HomePlanet = tatooineHomeplant; baseCharacter.CharacterFriends = new CharacterFriend[] { new CharacterFriend { Character = baseCharacter, CharacterId = baseCharacter.Id, Friend = friendCharacter, FriendId = friendCharacter.Id } }; Character[] entities = new Character[] { baseCharacter }; var dbContextMock = new DbContextMock <TestDbContext>(_options); dbContextMock.CreateDbSetMock(x => x.Characters, (x, _) => (x.Id), entities); var repository = new ReadAsyncRepository <Character>(dbContextMock.Object); // Act Character[] characters = await repository.GetAllAsync(new WithFriendsFromTatooineSpecification()); // Assert characters.Should().NotBeNull().And.NotBeEmpty(); }
public async Task Should_SetCriteria() { // 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 ReadAsyncRepository <TestEntity>(dbContextMock.Object); // Act TestEntity[] result = await repository.GetAllAsync(new TestEntityCriteriaSpecification(entities[0].Id)); // Assert result.Should().NotBeNull().And.BeEquivalentTo(entities[0]); }
public async Task ListAllAsync_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 ReadAsyncRepository <TestEntity>(dbContextMock.Object); // Act TestEntity[] result = await repository.GetAllAsync(); // Assert result.Should().NotBeNull().And.HaveCount(2); }
public async Task Should_OrderByNameDescending() { // Arrange TestEntity[] entities = _fixture.CreateMany <TestEntity>(4).ToArray(); var dbContextMock = new DbContextMock <TestDbContext>(_options); dbContextMock.CreateDbSetMock(x => x.TestEntities, (x, _) => (x.Id), entities); var repository = new ReadAsyncRepository <TestEntity>(dbContextMock.Object); // Act TestEntity[] result = await repository.GetAllAsync(new TestEntityOrderByNameDescSpecification()); TestEntity[] orderedList = entities.OrderByDescending(o => o.Name).ToArray(); // Assert result.Should().Equals(orderedList); }
public async Task Should_ApplyPaging() { // Arrange TestEntity[] entities = _fixture.CreateMany <TestEntity>(4).ToArray(); var dbContextMock = new DbContextMock <TestDbContext>(_options); dbContextMock.CreateDbSetMock(x => x.TestEntities, (x, _) => (x.Id), entities); var repository = new ReadAsyncRepository <TestEntity>(dbContextMock.Object); // Act TestEntity[] result = await repository.GetAllAsync(new TestEntityPaginatedSpecification(1, 2)); // Assert result.Should().HaveCount(2); result[0].Id.Should().Equals(entities[0]); result[1].Id.Should().Equals(entities[1]); }
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); var repository = new ReadAsyncRepository <TestEntity>(dbContextMock.Object); var specification = new Mock <ISpecification <TestEntity> >(); // Act TestEntity[] result = await repository.GetAllAsync(specification.Object); // Assert result.Should().NotBeNull().And.BeOfType(typeof(TestEntity[])); }