Actually, the code from my previous post was in a different class, not the
test class itself.  I was creating a new instance of that class and manually
resolving the ITransactionManager parameter.

After reading your post, I thought I would try it on the test class itself.
The root that the container lives in is the test assembly initializer, which
I call TestHarness.

    [TestClass]
    public static class TestHarness
    {
        public static IWindsorContainer Container { get; set; }

        [AssemblyInitialize]
        public static void Initialize(TestContext testContext)
        {
            Container = new WindsorContainer();
            Container.Install(new Registrar());
        }

        [AssemblyCleanup]
        public static void Cleanup()
        {
            Container.Dispose();
        }
    }

I still can't use constructor injection directly in the real test though,
because the test runner will always expect a parameterless constructor.  So
my test method looks something like this:

    [TestClass]
    public class MyTests
    {
        [TestMethod]
        [Transaction]
        public void SomeTest()
        {
            // do some stuff

            // roll back the transaction
            var manager =
TestHarness.Container.Resolve<ITransactionManager>();
            var transaction = manager.CurrentTransaction.Value;
            transaction.Rollback();
        }
    }


This should work, but it doesn't.  I do indeed get a manager, but the
transaction is null, so rollback fails.

I can dig deeper into the source for AutoTx if someone can point me in the
right direction.  Where does the transaction actually get created?  In other
words, what part of AutoTx actually looks for the [Transaction] attribute?
I have a feeling that part of the code is not being invoked in my scenario.

Thanks,
-Matt


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Michael McCurrey
Sent: Tuesday, November 08, 2011 5:16 PM
To: Castle Project Users
Subject: Re: How to explicitly roll back when using [Transaction] attribute
in AutoTx Facility?

This may be too obvious; but I notice from your comment that your
ITransactionManager field instance is constructor injected, is your
unit-test invoking your class from that constructor?

On Nov 7, 12:00 pm, Matt Johnson <[email protected]> wrote:
> Ok, I found that once I resolve an ITransactionManager, I can get the
> current transaction and roll it back:
>
>   // constructor injected
>   private readonly ITransactionManager _transactionManager;
>
>   ...
>
>   [Transaction]
>   public void SomeMethod()
>   {
>     ...
>     _transactionManger.CurrentTransaction.Value.Rollback();
>   }
>
> This works fine in both a console app and a WCF service.  HOWEVER - it
> doesn't seem to work within a unit test!  The main reason I want a
> rollback option is so I can create unit tests for the data layer and
> roll back the transactions after the test so the database is left
> unmodified.  In a unit test (at least when using MSTest in
> VisualStudio), CurrentTransaction is null.  I can't figure out why!
>
> Any ideas?
> -Matt
>
> On Nov 4, 12:57 pm, Matt Johnson <[email protected]> wrote:
>
>
>
>
>
>
>
> > I'm trying out the AutoTx facility.  I have it working where I apply
> > the [Transaction] attribute to my class and it works great when I want
> > the transaction to commit if there are no exceptions.
>
> > But how do I explicitly get the transaction to roll back without
> > throwing an exception?  Where is the transaction context?
>
> > I would have thought Session.Transaction.Rollback() would work, but
> > it's not the same transaction.
>
> > Thanks!
> > -Matt

-- 
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en.

Reply via email to