コード例 #1
0
 public void Find_with_some_matched()
 {
     using (var context = new BloggingContext(_serviceProvider, _contextOptions))
     {
         var service = new BlogService(context);
         var result = service.Find("cat");
         Assert.AreEqual(2, result.Count());
     }
 }
コード例 #2
0
 public void Find_with_unmatched_term()
 {
     using (var context = new BloggingContext(_serviceProvider, _contextOptions))
     {
         var service = new BlogService(context);
         var result = service.Find("horse");
         Assert.AreEqual(0, result.Count());
     }
 }
コード例 #3
0
        public void Add_writes_to_database()
        {
            // All contexts that share the same service provider will share the same InMemory database
            var options = CreateNewContextOptions();

            // Run the test against one instance of the context
            using (var context = new BloggingContext(options))
            {
                var service = new BlogService(context);
                service.Add("http://sample.com");
            }

            // User a seperate instance of the context to verify correct data was saved to database
            using (var context = new BloggingContext(options))
            {
                Assert.AreEqual(1, context.Blogs.Count());
                Assert.AreEqual("http://sample.com", context.Blogs.Single().Url);
            }
        }
コード例 #4
0
        public BlogServiceTestsReadOnly()
        {
            // Create a service provider to be shared by all test methods
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddEntityFramework().AddInMemoryDatabase();
            _serviceProvider = serviceCollection.BuildServiceProvider();

            // Create options to tell the context to use the InMemory database
            var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
            optionsBuilder.UseInMemoryDatabase();
            _contextOptions = optionsBuilder.Options;

            // Insert the seed data that is expected by all test methods
            using (var context = new BloggingContext(_serviceProvider, _contextOptions))
            {
                context.Blogs.Add(new Blog { Url = "http://sample.com/cats" });
                context.Blogs.Add(new Blog { Url = "http://sample.com/catfish" });
                context.Blogs.Add(new Blog { Url = "http://sample.com/dogs" });
                context.SaveChanges();
            }
        }
コード例 #5
0
        public void Find_searches_url()
        {
            // All contexts that share the same service provider will share the same InMemory database
            var options = CreateNewContextOptions();

            // Insert seed data into the database using one instance of the context
            using (var context = new BloggingContext(options))
            {
                context.Blogs.Add(new Blog { Url = "http://sample.com/cats" });
                context.Blogs.Add(new Blog { Url = "http://sample.com/catfish" });
                context.Blogs.Add(new Blog { Url = "http://sample.com/dogs" });
                context.SaveChanges();
            }

            // Use a clean instance of the context to run the test
            using (var context = new BloggingContext(options))
            {
                var service = new BlogService(context);
                var result = service.Find("cat");
                Assert.AreEqual(2, result.Count());
            }
        }
コード例 #6
0
 public BlogService(BloggingContext context)
 {
     _context = context;
 }
コード例 #7
0
 public BlogService(BloggingContext context)
 {
     _context = context;
 }