Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
897 views
in Technique[技术] by (71.8m points)

c# - Moq and setting up DB Context

I have an Entity Framework DB Context file. I am trying to setup a Moq framework in NUnit. Currently receiving error below for Moq Nunit test. How would I setup the DBContext, and add items to a Product Table?

"No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext."

Electronics DB Context file

public partial class ElectronicsContext : DbContext
{
    public ElectronicsContext()
    {
    }

    public ElectronicsContext(DbContextOptions<ElectronicsContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Product> Product { get; set; }
    public virtual DbSet<ProductCategory> ProductCategory { get; set; }

Startup.cs

    var connection = @"Server=localhost;Database=Electronics;Trusted_Connection=True;ConnectRetryCount=0";
    services.AddDbContext<ElectronicsContext>(options => options.UseSqlServer(connection));

Moq Nunit Test

 [SetUp]
 public void Setup()
 {
    var ElectronicsContext = new Mock<ElectronicsContext>();
    var ProductRepository = new Mock<ProductRepository>();

    Product producttest = new Product();
    _dbContext.Product.Add(new Product {ProductId = 1, ProductName = "TV", ProductDescription = "TV testing",ImageLocation = "test"});
    _dbContext.SaveChanges();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need to mock the context in unit tests. You should use the DbContextOptions class to specify you want to use an in memory database to run your tests against.

[TestMethod]
public void TestProducts()
{
    var options = new DbContextOptionsBuilder<ElectronicsContext>()
        .UseInMemoryDatabase(databaseName: "Products Test")
        .Options;

    using(var context = new ElectronicsContext(options))
    {
        context.Products.Add(new Product {ProductId = 1, ProductName = "TV", ProductDescription = "TV testing",ImageLocation = "test"});
        context.SaveChanges();
    }

    using(var context = new ElectronicsContext(options))
    {
        // run your test here

    }
}

This runs against the in-memory representation of your database instead of relying on the physical server. The connection string you provided in the startup.cs is not used as part of the tests.

More info can be found here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...