我有一个使用.NET Framework 4.6.1和EF6的ASP.NET Core项目。 现在我想编写一些单元测试,并且已花费数小时来配置内存中的SQLite数据库以使用EF6。但它没有用。
所以,问题是如何在没有任何模拟(而非内存数据库)的情况下使用EF6测试我的项目?
我目前的代码:
public class DataAccessLayer : DbContext
{
public DataAccessLayer(string connectionString)
: base(connectionString) {
}
public DataAccessLayer(DbConnection connection)
: base(connection, true) {
}
public DbSet<User> Users { get; set; }
public DbSet<Setting> Settings { get; set; }
public DbSet<UserRole> UserRoles { get; set; }
public DbSet<MainKey> MainKeys { get; set; }
}
[Table("Users")]
public class User
{
[Key]
[Required]
public int UserID { get; set; }
[Required][StringLength(50)]
public string UserName { get; set; }
...
}
public class Testbase
{
protected DataAccessLayer Context { get; private set; }
[TestInitialize]
public virtual void SetUp()
{
var connection = this.CreateConnection();
connection.Open();
this.Context = new DataAccessLayer(connection);
this.Context.Database.CreateIfNotExists();
}
private SQLiteConnection CreateConnection() {
var connectionStringBuilder = new SQLiteConnectionStringBuilder { DataSource = ":memory:" };
return new SQLiteConnection(connectionStringBuilder.ToString());
}
}
如果我尝试添加用户,则会收到以下错误:
System.Data.SQLite.SQLiteException:SQL逻辑错误或缺少数据库 没有这样的表:用户。
我假设我的表是通过调用this.Context.Database.CreateIfNotExists();
生成的,还是我错了?
答案 0 :(得分:4)
考虑使用Nuget包努力
这是一个简单快速的内存数据库,用于单元测试。
您可以使用空数据库启动它并使用数据库播种器自行填充,也可以使用测试CSV文件中的值填充它。
请参阅Tutorials Effort - Entity Framework Unit Testing Tool
带有博客和帖子的数据库的简单示例。博客和帖子之间的一对多关系
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
public class BloggingContext : DbContext
{
public BloggingContext() : base() { } // constructor using config file
public BloggingContext(string nameOrConnectionString) : base(nameOrConnectionString) { }
public BloggingContext(DbConnection connection) : base(connection, true) { }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
您没有获得数据库的连接字符串,而是获得DbConnection。因此,BloggingContext的第三个构造函数。
在此构造函数中传递给超类的布尔值是告诉DbContext它拥有连接:DbContext应该关闭并在它处理时处理连接。
这是您使用普通DbContext的唯一区别。对DbContext和DbSets的所有其他调用都是正常的。
实施例
static void Main(string[] args)
{
var connection = Effort.DbConnectionFactory.CreateTransient();
using (var dbContext = new BloggingContext(connection))
{
var addedBlog = dbContext.Blogs.Add(new Blog[]
{
Name = "1",
Posts = new Post[]
{
new Post() {Title = "1st", Content = "a"},
new Post() {Title = "2nd", Content = "b"},
new Post() {Title = "3rd", Content = "c"},
},
});
dbContext.SaveChanges();
}
using (var dbContext = new BloggingContext(connection))
{
var allPosts = context.Posts.ToList();
foreach (var post in allPosts)
{
Console.WriteLine($"{post.Id}: {post.Title}");
}
}
一个提示:在开发过程中,有时很难看到测试是否由于不正确的测试(数据)或由于测试的代码不正确而失败。在测试期间调试期间检查数据库中的内容是相当困难的。因此,我倾向于使用填充了测试值的真实数据库来开发测试,并且一旦很少需要调试测试,就切换到内存数据库。实际上,对我来说,这是第二个或第三个DbContext构造函数之间的切换