.NET Framework 4.6.1およびEF6を使用したASP.NETコアプロジェクトがあります。 EF6を使用するためのメモリ内SQLiteデータベースを構成するために、単体テストを書いて、すでに時間を費やしたいと思います。しかし、それは動作しません。EF6(Entity Framework 6)を使用した単体テストの作成
質問はどのように私は私のプロジェクトをテストすることができますか?
私の現在のコード:
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 logic error or missing database no such table: Users.
私は私のテーブルはthis.Context.Database.CreateIfNotExists();
を呼び出すことによって生成されたを想定、または私はそれについて誤解していますか?
質問には答えられませんが、https://github.com/tamasflamich/effortを考慮する必要があります – Jim
FWIW、メモリデータベースまたはいいえ、これは単体テストではありません。 *統合テスト*です。その点では、実際のサイトが実行されているDBプラットフォーム(SQLiteではなくSQL Serverなど)を実際に使用する必要があります。 –