0
public class RollBack : OnMethodBoundaryAspect // or another AOP for meth interception
{
    public override void OnEntry(MethodExecutionEventArgs eventArgs)
    {
        try
        {
            ServiceConfig cfg = new ServiceConfig();
            cfg.Transaction = TransactionOption.RequiresNew;
            cfg.TrackingAppName = "Application Unit Tests";
            cfg.TransactionDescription = "Application Unit Tests Transaction";
            cfg.TransactionTimeout = 0x2710;
            ServiceDomain.Enter(cfg);
        }
        catch (Exception exception)
        {
            Console.WriteLine("Could not enter into a new transaction:\n" + exception);
        }
    }

    public override void OnExit(MethodExecutionEventArgs eventArgs)
    {
        try
        {
            if (ContextUtil.IsInTransaction)
            {
                ContextUtil.SetAbort();
            }
            ServiceDomain.Leave();
        }
        catch (Exception exception)
        {
            Console.WriteLine("Could not leave an existing transaction:\n" + exception);
        }
    }
}
4

1 回答 1

0

I can see that you are tactically testing within a transaction to roll back after testing.

I personally start from scratch and create the tables for the test and remove them afterwards. Another common technique is to restore a database to a known state - although I would suggest that this indicates your tests are too wide if they rely on lots of state in the database.

I wrote about this for PHP but I work more often in .NET.

http://www.stevefenton.co.uk/Content/Blog/Date/201110/Blog/Database-Integration-Testing-With-Enhance-PHP/

于 2013-02-04T21:55:03.933 回答