実際にデータに影響を与えずにデータベースにストアドプロシージャのテストをいくつか実行したい(または、テストの実行後に永続的な影響を与えることなく) 。DBユニットテストでTransactionScCommunicationExceptionが発生する
は、いくつかの研究の後、私は、私は単一のテストメソッド内でこのすべてを置くよう今これは罰金限り動作します私のVisual Studio 2010のテストプロジェクトなど
using(new TransactionScope())
{
using(SqlConnection connection = new SqlConnection("someConnectionString"))
{
connection.Open();
using(SqlCommand command = new SqlCommand("some sql", connection))
{
// Do some database stuff...
}
}
}
内のTransactionScopeを使用してのアプローチを思い付いつまり、TransactionScopeの使用ブロックが終了すると、データベースに対するすべての変更は自動的にロールバックされます。
私の問題は、ClassInitializeでデータベースの処理をしたいので、テストクラスごとに1回だけ行う必要があります。共有のTransactionScopeプロパティを作成し、ClassInitializeメソッドでTransactionScopeのインスタンスを割り当てると、これは問題なく動作します。テストメソッドのいずれかでデータベース関連の処理を行うと、そのメソッド内でTransactionManagerCommunicationExceptionが発生します。
私はそれが事実であることをよく理解していませんし、私のアプローチに間違いがあるかどうか、あるいはすべてのセットを含むTransactionScopeをセットアップする必要なしに、各テストメソッド内のテスト用のテスト用のコンポーネントを再度作成します。
以下のコードの抜粋
EDITが、私はこれが十分な情報を与える願っています:
public TransactionScope Scope { get; set; }
[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
Scope = new TransactionScope();
// Do some db set up stuff, e.g. create records used for tests etc.
}
[ClassCleanup]
public static void ClassCleanup()
{
Scope.Dispose();
}
[TestMethod]
public void MyTestMethod()
{
using(SqlConnection connection = new SqlConnection("someConnectionString"))
{
DataTable result = new DataTable();
using(SqlCommand command = new SqlCommand("spName", connection))
{
command.CommandType = CommandType.StoredProcedure;
using(SqlDataAdapter adapter = new SqlDataAdapter())
{
adapter.SelecteCommand = command;
// The next line causes the exception to be thrown
adapter.Fill(result);
}
}
// Assertions against DataTable result
}
}
例外が
TransactionManagerCommunicationExceptionあるユーザーコードによって分散のための ネットワークアクセス未処理でしたトランザクションマネージャ(MSDTC)が無効になっています。コンポーネントサービスの管理ツールを使用して、MSDTCのセキュリティ構成でネットワークアクセス用のDTCを有効にしてください。
私は設定を試してみて、変更される可能性があることを理解し、私は例外で始まることを得るなぜ私は理解していない - シングル(テスト)の方法では、上記のコードを持つと比べて異なるが何でありますか?可能性、あなたが取得しているエラーについては
void Main()
{
using(new SetupTransaction())
{
//Your test
}
}
public class SetupTransaction : IDisposable
{
private TransactionScope transaction;
public SetupTransaction()
{
transaction = new TransactionScope();
//Do your stuff here
}
public void Dispose()
{
transaction.Dispose();
}
}
:事前に
おかげで、
よろしく
G.
例外から詳細を提供できますか? –