Reputation: 152
So, I am trying to Unit Test my project, and the UnitTests.cs file has 8 tests, out of which 6 pass but 2 fail, the two that fail gives me the error of "No database provider has been configured for this DbContext." the UnitTests.cs file:
private readonly CatalogItemContext _context;
private Microsoft.EntityFrameworkCore.DbContextOptionsBuilder<CatalogItemContext> obj;
public UnitTests()
{
obj = new DbContextOptionsBuilder<CatalogItemContext>();
_context = new CatalogItemContext(obj.Options);
}
[Fact]
public void Get_WhenCalled_ReturnsAllItems()
{
CatalogController cc = new CatalogController(_context);
var test = cc.GetItems().Result;
var okResult = test.Result as OkObjectResult;
var items = Assert.IsType<List<CatalogItem>>(okResult.Value);
Assert.Equal(3, items.Count);
}
[Fact]
public void Get_WhenCalled_ReturnsAllProductsTypes()
{
CatalogController cc = new CatalogController(_context);
var test = cc.GetTypes().Result;
var okResult = test.Result as OkObjectResult;
var Expected = typeof(OkObjectResult);
var items = Assert.IsType<List<CatalogType>>(okResult.Value);
Assert.Equal(3, items.Count);
}
My Startup.cs contains the following:
string connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<CatalogItemContext>(opt => opt.UseSqlServer(connectionString));
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
And Context File is as follows:
public class CatalogItemContext : DbContext
{
public CatalogItemContext(DbContextOptions<CatalogItemContext> options) : base(options)
{
}
public DbSet<CatalogItem> CatalogItem { get; set; }
public DbSet<CatalogType> CatalogType { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.HasDefaultSchema("Catalog");
builder.ApplyConfiguration(new CatalogTypeEntityTypeConfiguration());
builder.ApplyConfiguration(new CatalogItemEntityTypeConfiguration());
}
}
public class CatalogContextDesignFactory : IDesignTimeDbContextFactory<CatalogItemContext>
{
public CatalogItemContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<CatalogItemContext>()
.UseSqlServer("--REDACTED CONNECTION STRING--",
x => x.MigrationsHistoryTable("__EFMigrationHistory", "catalog"));
return new CatalogItemContext(optionsBuilder.Options);
}
Can someone please shed some light on what I can do to make it work?
EDIT: Error which are appearing: PS: I am using VSCode.
[xUnit.net 00:00:01.26] UnitTests.Get_WhenCalled_ReturnsAllProductsTypes [FAIL]
[xUnit.net 00:00:01.27] UnitTests.Get_WhenCalled_ReturnsAllItems [FAIL]
X UnitTests.Get_WhenCalled_ReturnsAllProductsTypes [2ms]
Error Message:
System.AggregateException : One or more errors occurred. (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<TContext> object in its constructor and passes it to the base constructor for DbContext.)
---- System.InvalidOperationException : 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<TContext> object in its constructor and passes it to the base constructor for DbContext.
Stack Trace:
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at UnitTests.Get_WhenCalled_ReturnsAllProductsTypes() in --Location redacted--\Test\UnitTests.cs:line 33
----- Inner Stack Trace -----
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
at Microsoft.EntityFrameworkCore.DbContext.get_Model()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.Microsoft.EntityFrameworkCore.Query.Internal.IAsyncEnumerableAccessor<TEntity>.get_AsyncEnumerable()
at Microsoft.EntityFrameworkCore.Extensions.Internal.QueryableExtensions.AsAsyncEnumerable[TSource](IQueryable`1 source)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at --Redacted--.Controllers.CatalogController.GetTypes() in --Location Redacted--\src\--Redacted--\Controllers\CatalogController.cs:line 43
X UnitTests.Get_WhenCalled_ReturnsAllItems [2ms]
Error Message:
System.AggregateException : One or more errors occurred. (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<TContext> object in its constructor and passes it to the base constructor for DbContext.)
---- System.InvalidOperationException : 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<TContext> object in its constructor and passes it to the base constructor for DbContext.
Stack Trace:
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at UnitTests.Get_WhenCalled_ReturnsAllItems() in --Location Redacted--\src\--Redacted--\Test\UnitTests.cs:line 22
----- Inner Stack Trace -----
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
at Microsoft.EntityFrameworkCore.DbContext.get_Model()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.Microsoft.EntityFrameworkCore.Query.Internal.IAsyncEnumerableAccessor<TEntity>.get_AsyncEnumerable()
at Microsoft.EntityFrameworkCore.Extensions.Internal.QueryableExtensions.AsAsyncEnumerable[TSource](IQueryable`1 source)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at --Redacted--.Controllers.CatalogController.GetItems() in --Location Redacted--\src\Redacted\Controllers\CatalogController.cs:line 32
Test Run Failed.
Total tests: 8
Passed: 6
Failed: 2
Total time: 2.1150 Seconds
Upvotes: 0
Views: 3192
Reputation: 12859
Compare these two pieces of code :
obj = new DbContextOptionsBuilder<CatalogItemContext>();
_context = new CatalogItemContext(obj.Options);
and
var optionsBuilder = new DbContextOptionsBuilder<CatalogItemContext>()
.UseSqlServer("--REDACTED CONNECTION STRING--",
x => x.MigrationsHistoryTable("__EFMigrationHistory", "catalog"));
return new CatalogItemContext(optionsBuilder.Options);
One has something the other is missing.
Also note that the code in Startup.cs is never involved in execution of your tests. I recommend putting breakpoints in code in Startup.cs and in CatalogContextDesignFactory.CreateDbContext
and see if they are hit when you debug the tests.
Upvotes: 2