現象
- 個別実行の単体テストは成功する
- 「すべて実行」は失敗する
- EntityFramework関連のエラーが発生する
エラーメッセージ例
"The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information."
または、
The Entity Framework provider type 'Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342' registered in the application config file for the ADO.NET provider with invariant name 'Oracle.ManagedDataAccess.EntityFramework' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application
解決策
いくつか条件によって異なります。
- 正しくEntityFrameworkをnugetでインストールできているか確認する。
- web.config/app.configに正しく設定できているか確認する。
- [DeploymentItem]属性で外部ファイルを単体テストでロードしているか確認する。
3番目については正しく理解できていませんが、これを書いた途端にテストが失敗し出したので調べてみたら、どうやら関連がありそうでした。↓
DeploymentItem breaks EntityFramework unit test in separate assembly
とりあえず、意図的にSystem.Data.Entity.SqlServer.SqlProviderServices
をDbContextで宣言すればテストは動きそうです。
が、外部ファイルのプロパティで出力ディレクトリにコピーを「常にコピーする」にすれば単体テストで読み込めたので、いったん[DeploymentItem]を使わずにおきます。(単体テストを自動化した際にもうまく動くかは未確認です。)