Exemple #1
0
        public async Task GetBlogsByPerson_MultipleBlogPosts_BlogPostsWithPersonPopulated()
        {
            var person    = FakesFactory.Create <Person>();
            var blogPosts = Enumerable.Range(1, 3)
                            .Select(x => FakesFactory.Create <BlogPost>())
                            .Select(x =>
            {
                x.Person = person;
                return(x);
            });

            await _DatabaseFixture.PerformDatabaseOperation(async context =>
            {
                await context.Persons.AddAsync(person);
                await context.BlogPosts.AddRangeAsync(blogPosts);
                await context.SaveChangesAsync();
            });

            await _DatabaseFixture.PerformDatabaseOperation(async context =>
            {
                var myService = new MyService(context);

                var blogs = await myService.GetBlogsByPerson(person.Name);

                Assert.NotNull(blogs.First().Person);
            });
        }
Exemple #2
0
 public async Task DbContextWithDependency_MissingCorrectConstructor_ExceptionThrown()
 {
     var databaseFixture = new DatabaseFixture <DbContextWithDependency>();
     await Assert.ThrowsAsync <InvalidOperationException>(async() =>
     {
         await databaseFixture.PerformDatabaseOperation(_ => { });
     });
 }
Exemple #3
0
        public async Task DbContextWithInvalidConstructor_ThrowsException()
        {
            var databaseFixture = new DatabaseFixture <HasInvalidConstructor>();

            var ex = await Assert.ThrowsAsync <InvalidOperationException>(() =>
                                                                          databaseFixture.PerformDatabaseOperation(context => { }));

            Assert.Equal($"'{typeof(HasInvalidConstructor)}' does not contain constructor that has a valid signature", ex.Message);
        }
Exemple #4
0
        public async Task DbContextWithDependency_DependencyByTypeSpecified_ContextCreated()
        {
            var dependency      = new Dependency();
            var databaseFixture = new DatabaseFixture <DbContextWithDependency>();

            databaseFixture.AddDependency <IDependency>(dependency);

            await databaseFixture.PerformDatabaseOperation(context =>
            {
                Assert.Equal(dependency, context.MyDependency);
            });
        }
Exemple #5
0
        public async Task DbContextWithDependency_DependencyByNameSpecified_ContextCreated()
        {
            var dependency1     = new Dependency();
            var dependency2     = new Dependency();
            var databaseFixture = new DatabaseFixture <HasDuplicateTypeDependencies>();

            databaseFixture.AddDependency <IDependency>(dependency1, "dependency1");
            databaseFixture.AddDependency <IDependency>(dependency2, "dependency2");

            await databaseFixture.PerformDatabaseOperation(context =>
            {
                Assert.Equal(dependency1, context.Dependency1);
                Assert.Equal(dependency2, context.Dependency2);
            });
        }
Exemple #6
0
        public void DatabaseFixture_HookIntoBeforeLoggingSetup_EventExecuted()
        {
            var databaseFixture = new DatabaseFixture <SampleDbContext>();

            bool operationPerformed = false;

            databaseFixture.BeforeLoggingSetup += (sender, builder) =>
            {
                operationPerformed = true;
            };

            databaseFixture.PerformDatabaseOperation(_ => Task.CompletedTask);

            Assert.True(operationPerformed);
        }
        public async Task DatabaseFixture_CanPerformDatabaseOperation()
        {
            var person = FakesFactory.Create <Person>();

            await _DatabaseFixture.PerformDatabaseOperation(async context =>
            {
                context.Persons.Add(person);
                await context.SaveChangesAsync();
            });

            await _DatabaseFixture.PerformDatabaseOperation(context =>
            {
                var personInDatabase = context.Persons.SingleOrDefault(x => x.PersonId == person.PersonId);

                Assert.NotNull(personInDatabase);
                Assert.Equal(person.Age, personInDatabase.Age);
                Assert.Equal(person.Name, personInDatabase.Name);

                return(Task.CompletedTask);
            });
        }