This is how I've been doing it (without too much thought) but after reading
Berryl's post I was hoping there better way that didn't require setting the
Expect for a function *and* Asserting it was called. Particularly when the
function has parameters with Arg Matches that have to be set twice. But I
can't quite see how that would be possible.

I guess in this case the AAA cake takes a little more baking :)

Mark

-----Original Message-----
From: [email protected] [mailto:[email protected]] On
Behalf Of Adam Tybor
Sent: Thursday, 9 July 2009 11:26 a.m.
To: Rhino.Mocks
Subject: [RhinoMocks] Re: can I do this AAA style?


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