gpt4 book ai didi

c# - 测试时的努力问题

转载 作者:太空宇宙 更新时间:2023-11-03 15:17:30 24 4
gpt4 key购买 nike

我一直在阅读有关使用 Effort 进行单元测试的文章,因为在内存中设置数据库非常“容易”。我一直在尝试在我的测试数据服务中实现这一点,但我一直遇到问题。我正在使用 Entity Framework 6 和从现有表生成的模型。

上下文模型

public partial class Entities : DbContext
{
public Entities()
: base("name=Entities")
{
}

//Overloading for effort unit test
public Entities(DbConnection connection) : base(connection,true)
{

}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//throw new UnintentionalCodeFirstException();
}

public virtual DbSet<REPAIR_CHECK_IN_TABLE> REPAIR_CHECK_IN_TABLEs { get; set; }
public virtual DbSet<tblPlanarWarranty> tblPlanarWarranties { get; set; }
}

测试数据服务

private IDataLoader loader;
private Entities context;


public TestDataService()
{

loader = new Effort.DataLoaders.CsvDataLoader(@"_TestDataFiles\");
context = new Entities(Effort.DbConnectionFactory.CreateTransient(loader));
}

最初我会收到以下错误。

No entity Framework provider for the ADO.NET provider withe invariant name 'Effort.Provider'

然后我将所述提供程序添加到应用配置文件的我的提供程序部分并开始收到此错误。

The 'Instance' member of the entity Framework provider type 'Effort.ProviderServices,Effort,Version-1.0.0.0 didt not return an object that inherits from 'System.Data.Entity.Core.Common.DBProviderServices'

有没有人知道我做错了什么?在网站上,它就像几行一样简单,但这已经变成了一个巨大的困惑。

最佳答案

小心!使用 EF6 时,不要安装 Effort by ZZZ Projects!

通过相同的 ZZZ 项目安装 Effort.EF6

class SchoolDbContext : DbContext
{
// the standard constructors you probably already use:
public SchooldDbContext() : base(...) {}
public SchoolDbContext(string nameOrConnectionString) : base(nameOrConnectionString) {}

// add this one for effort:
public SchoolDbContext(DbConnection existingConnection, bool contextOwnsConnection)
: base(existingConnection, contextOwnsConnection) { }

... // DbSets, etc

使用干净的空数据库

var dbConnection = Effort.DbConnectionFactory.CreateTransient();
using (var dbContext = new SchoolDbContext(dbConnection, true)
{
dbContext.Schools.Add(new School {...});
dbConext.SaveChanges();

// this database exists as long as the connection exists
var schoolCount = dbContext.Schools.Count();
// expect one school
}

// a new empty database will be created: there won't be any schools
dbConnection = Effort.DbConnectionFactory.CreateTransient();
using (var dbContext = new SchoolDbContext(dbConnection)
{
var schoolCount = dbContext.Schools.Count();
// expect no schools
}

重用数据库

此数据库将在您的程序运行期间保留。该模型只会创建一次。

const string dbName = "MyDb"
var dbConnection = Effort.DbConnectionFactory.CreatePersistent(dbName);
using (var dbContext = new SchoolDbContext(dbConnection)
{
dbContext.Schools.Add(new School {...});
dbContext.SaveChanges();
}

dbConnection = Effort.DbConnectionFactory.CreatePersistent(dbName);
using (var dbContext = new SchoolDbContext(dbConnection)
{
var schoolCount = dbContext.Schools.Count();
// expect the school you just added
}

关于c# - 测试时的努力问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38441528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com