Assembly: MbUnit (in MbUnit.dll) Version: 3.2.0.0 (3.2.528.0)
Syntax
| C# |
|---|
public class RollbackAttribute : TestDecoratorPatternAttribute |
| Visual Basic (Declaration) |
|---|
Public Class RollbackAttribute _ Inherits TestDecoratorPatternAttribute |
Remarks
By default, the attribute does not rollback changes performed during SetUp and TearDown. Set the IncludeSetUpAndTearDown to true to change this.
The attribute uses a TransactionScope to set an ambient transaction that will be rolled back. Assuming the subject under test enlists itself with this transaction then its transactions operations will be rolled back.
Unfortunately there are a few caveats from this approach, the subject under test might use its own transaction manager and ignore the ambient transaction set up here. Likewise, it is possible that the presence of an enclosing transaction scope can interfere with the normal operation of the subject under test.
If this attribute does not work for a particular application, we recommend that you create your own custom rollback decorator attribute with the appropriate application-specific semantics. Just create a new subclass of TestDecoratorAttribute and override the Execute(PatternTestInstanceState) method to wrap test execution as required.
Here are a few troubleshooting tips in case Rollback is not working properly for your application:
- Ensure that the subject under test enlists its work in the current "ambient" transaction managed by the TransactionManager. Not all databases support these transactions.
- Ensure that the Distributed Transaction Coordinator service is enabled and running if you are using COM+ transactions.
- Ensure that the database is configured to enable distributed transactions. This feature is sometimes disabled for performance reasons to prevent long-running distributed transactions from locking database resources.
Examples
The following example shows the RollbackAttribute in use:
CopyC#[TestFixture]
public class RollbackTest {
[Test, Rollback]
public void TestWithRollback()
{
// Any transaction performed within this test will be rolled back
// automatically when the test completes.
}
[Test, Rollback(IncludeSetupAndTearDown=true)]
public void TestWithRollback()
{
// Any transaction performed within this test or within its setup and
// teardown will be rolled back automatically when the test completes.
}
}
Inheritance Hierarchy
System..::.Attribute
Gallio.Framework.Pattern..::.PatternAttribute
Gallio.Framework.Pattern..::.DecoratorPatternAttribute
Gallio.Framework.Pattern..::.TestDecoratorPatternAttribute
MbUnit.Framework..::.RollbackAttribute
