Do the following two tests not do what you want?

public void TestExistingSessionUsesExistingTransaction()
{
  // Arrange
  var session = MockRepository.GenerateMock<ISession>();
  var sut = new Sut(session);
  sut.HasExistingSession = true;

  // Act
  sut.TransactionSave("foo");

  // Assert
  session.AssertWasCalled(x => x.Save("foo"));
  session.AssertWasNotCalled(x => x.BeginTransaction());
}

public void TestNoExistingSessionBeginsNewTransaction()
{
  // Arrange
  var session = MockRepository.GenerateMock<ISession>();
  var tx = MockRepository.GenerateMock<ITransaction>();
  var sut = new Sut(session);
  sut.HasExistingSession = false;
  session.Expect(x => x.BeginTransaction()).Return(tx);

  // Act
  sut.TransactionSave("foo");

  // Assert
  session.AssertWasCalled(x => x.Save("foo");
  session.AssertWasCalled(x => x.BeginTransaction());
  tx.AssertWasCalled(x => x.Commit());
}

On Jul 8, 5:51 pm, Berryl Hesh <[email protected]> wrote:
> Hello:
>
> Here is a code snippet that I can test successfully without complete
> AAA:
>
> private void TransactionalSave(object propertyValue)
>         {
>             if (_hasExistingSession)
>                 _currentSession.Save(propertyValue);
>             else
>                 using (var tx = _currentSession.BeginTransaction())
>                 {
>                     _currentSession.Save(propertyValue);
>                     tx.Commit();
>                 }
>         }
>
> If I tell the mocked ISession to:
>             _session.Expect(x => x.BeginTransaction()).Return
> (MockRepository.GenerateStub<ITransaction>());
>            ....
>            _session.VerifyAllExpectations();
>
> then the using statement (tx) variable is successfully stubbed out to
> make for a useful test.
>
> I'd much prefer to use AssertWasCalled(x=>x.BeginTrans.., mo=>mo.Return
> (stub)) but this leaves the tx variable unstubbed, ruining the test
> with a NullRefException on tx.Commit(). Is there some way I can eat my
> AAA cake, or is this a situation where I just can't?
>
> Cheers,
> Berryl
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Rhino.Mocks" 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/RhinoMocks?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to