In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to unit test Entity Framework Core. It is very detailed and has certain reference value. Friends who are interested must finish reading it.
I. introduction
Let's first explain how to unit test EntityFrameworkCore, here we use an in-memory database for testing. Using the in-memory database requires the installation of the Microsoft.EntityFrameworkCore.InMemory package.
Create a test project
Let's take the project created in the above article as an example to add a test project to the solution, where we choose to use xUnit as the test project:
The project structure after creation is shown in the following figure:
We first install the Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.InMemory packages in the EFCoreTest project and install them directly in NuGet. The installation steps are not described here. Add a reference to the entity after the addition is complete.
We create a data context that inherits from DbContext:
/ data context, inherited from DbContext/// public class EFCoreDbContext:DbContext {/ pass parameters / public EFCoreDbContext (DbContextOptions options): base (options) {} / / DbSet attribute public DbSet Blogs {get; set;}} to the parent class construction via the DbContextOptions option
We are creating a method with a return type of DbContextOptions:
Public static DbContextOptions CreateDbContextOptions (string databaseName) {var serviceProvider = new ServiceCollection (). AddEntityFrameworkInMemoryDatabase () .BuildServiceProvider (); var builder = new DbContextOptionsBuilder (); builder.UseInMemoryDatabase (databaseName) .UseInternalServiceProvider (serviceProvider); return builder.Options;}
Finally, write the test code, the overall code is as follows:
Using EFCore.Model;using Microsoft.EntityFrameworkCore;using Microsoft.Extensions.DependencyInjection;using System;using Xunit;namespace EFCoreTest {/ data context, inherited from DbContext/// public class EFCoreDbContext:DbContext {/ pass parameter / public EFCoreDbContext (DbContextOptions options): base (options) {} / / DbSet attribute public DbSet Blogs {get; set to the parent construction via the DbContextOptions option }} public class UnitTest1 {public static DbContextOptions CreateDbContextOptions (string databaseName) {var serviceProvider = new ServiceCollection (). AddEntityFrameworkInMemoryDatabase () .BuildServiceProvider (); var builder = new DbContextOptionsBuilder (); builder.UseInMemoryDatabase (databaseName) .UseInternalServiceProvider (serviceProvider); return builder.Options } / Test method, here use asynchronous / [Fact] public async void Test1 () {var options= CreateDbContextOptions ("batabase"); var context = new EFCoreDbContext (options); / / add data context.Blogs.Add (new Blog () {Name = "ef core"}) / / Save context.SaveChanges (); / / query data var blog = await context.Blogs.FirstOrDefaultAsync (p = > p.Id = = 1); / / assert Assert.NotNull (blog);}
Right-click on the test method and select "Live Unit Testing". This is real-time. We can see the real-time information in the output window:
Start the test and view the test results in the output window:
You can see that the test code has been ticked in front of it, indicating that the test passed. We modify the test code to query the data with an id of 2:
Because we only added one piece of data, not the one with id 2, the test reported an error.
At this point, we have completed a simple unit test.
The above is all the content of the article "how to Unit Test Entity Framework Core". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.