私はasp.net mvc web appのためのNUnitテストの初心者です。私は望むプロジェクトの同じデータベースに接続する必要があるテストプロジェクトからデータベースに接続するエンティティフレームワークを使ってテストしたいテストする 。NUnitからデータベースに接続するにはプロジェクトをテストしますか?
コントローラは次のようになります。私は以下のようにNUnitのテストを記述しようとしています
public class EXController : Controller
{
private readonly AppDbContext _context;
private readonly CommonDbContext _commonContext;
public EXController(CommonDbContext commonContext, AppDbContext context)
{
_commonContext = commonContext;
_context = context;
}
public ActionResult Index()
{
// some simple code.
}
}
:私はDLLとしてテストの下でプロジェクトを追加したテストプロジェクトの参照セクションでは は、私はすべてのクラス、私がテストしたいプロジェクトから アクセスすることができるしています。
CommonDbContextとAppDbContextは、データベースに接続するためにテスト対象のコードで使用しているクラスで、これらのクラスはconnectionstringとして引数をとります。
ですから、これらのクラスに接続文字列を渡して、Nunitテストプロジェクトからデータベースに接続します。
次の接続文字列を使用して、NUnitテストプロジェクトでデータベースに接続しようとしています。
CommonDbContext objcdb = new CommonDbContext("metadata =\"res://*/Data.CommonDbModel.CommonDbModel.csdl|res://*/Data.CommonDbModel.CommonDbModel.ssdl|res://*/Data.CommonDbModel.CommonDbModel.msl\";provider=MySql.Data.MySqlClient;provider connection string=\"server=servername;database=databasename;integratedsecurity=False;user id=userid;password=password");
AppDbContext Objadb = new AppDbContext("metadata = \"res://*/Data.AppDbModel.AppDbModel.csdl|res://*/Data.AppDbModel.AppDbModel.ssdl|res://*/Data.AppDbModel.AppDbModel.msl\";provider=MySql.Data.MySqlClient;provider connection string=\"server=servername;database=databasename;integratedsecurity=False;user id=userid;password=password");
// creating instance for the contrller I want to test
ExController obController = new CLINController(objcdb, Objadb);
NUnitテストプロジェクトからデータベースに接続しようとすると、以下の例外が発生します。
Exception:
An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Format of the initialization string does not conform to specification starting at index 164.
**Stack Trace:**
at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, String& keyname, String& keyvalue)
at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions.ParseInternal(IDictionary`2 parsetable, String connectionString, IList`1 validKeywords)
at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions..ctor(String connectionString, IList`1 validKeywords)
at System.Data.Entity.Core.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.Entity.Core.EntityClient.EntityConnection..ctor(String connectionString)
at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
at BidMaster.Web.Infrastructure.CurrentUser.get_User() in D:\BWM_sprint3_newcopy\BidMaster.Web\BidMaster.Web\Infrastructure\CurrentUser.cs:line 30
at BidMaster.Web.Controllers.CLINController.Index(Nullable`1 id) in D:\BWM_sprint3_newcopy\BidMaster.Web\BidMaster.Web\Controllers\CLINController.cs:line 51
at BidMaster.Test.Controllers.CLINControllerTests.IndexTest() in D:\BWM_sprint3_newcopy\BidMaster.Test\BidMaster.Test\Controllers\CLINControllerTests.cs:line 47
私が使用している接続文字列に問題がありますか?私を助けてください、ユニットテストプロジェクトからデータベースに接続する方法を説明してください。私はテストに新しいです。
ユニットテストプロジェクトのテスト対象プロジェクトに使用しているのと同じデータベースに接続する必要があります。なぜなら、私の要件は、ストアドプロシージャによって返されるデータをテストすることと、私がDBをモックした場合、ストアプロシージャで返されたデータをテストすることができませんでした。私はテスト中のプロジェクトと同じデータベースに接続する方法を教えてください。 – Satya