Create new mock repository ( and mock objects) for each test.
On Mon, Oct 6, 2008 at 11:42 PM, Ryan Vice <[EMAIL PROTECTED]> wrote:
>
> Hi,
>
> Below are two of my tests and then a utility method they use. If I
> don't call BackToRecordAll() before I call my SetupResults() function
> it will throw an error so I do a partially redundant call to
> BackToRecordAll() to reset all the expectations\results. I'd assume
> that the easy answer is to making my code less ugly would be to
> elminate using (_mockRepository.Record()) but i like the way the using
> block shows explicitly what is being mocked to newbies. Any other
> suggestions on how to make this look cleaner?
>
> --------------------------------
> Test code
> --------------------------------
>
> [Test]
> public void DecoderDisabled_UnableToShoot()
> {
> bool sourceGroupEnabled = false;
> bool sourceGroupEnabledWithDecoder = false;
>
> _mockRepository.BackToRecordAll();
> using (_mockRepository.Record())
> {
> SetupResults(sourceGroupEnabled,
> sourceGroupEnabledWithDecoder);
> }
>
> using (_mockRepository.Playback())
> {
> _dynamiteShooterSm.Init();
> }
>
> AssertThatCurrentStateIs(_dynamiteShooterSm.qUnableToShoot);
> }
>
> [Test]
> public void SourceGroupEnabledWithoutDecoder_UnableToShoot()
> {
> _mockRepository.BackToRecordAll();
> using (_mockRepository.Record())
> {
> SetupResults(true, false);
> SetupResult.For(_sourceGroup.Enabled).Return(false);
> }
>
> using (_mockRepository.Playback())
> {
> _dynamiteShooterSm.Init();
> }
> AssertThatCurrentStateIs(_dynamiteShooterSm.qUnableToShoot);
> }
>
> --------------------------------
> Setup code
> --------------------------------
>
> /// <summary>
> /// Method should be called from within a record block and
> /// will setup results for standard things needed during testing
> /// </summary>
> /// <param name="sourceGroupEnabled">The value returned by
> /// DynamiteShooterStateMachine.SourceGroup.Enabled</param>
> /// <param name="sourceGroupEnabledWithDecoders">The value
> returned by
> /// DynamiteShooterStateMachine.SourceGroup.EnabledWithDecoders</
> param>
> protected void SetupResults(bool sourceGroupEnabled,
> bool sourceGroupEnabledWithDecoders)
> {
> SetupResults("TestSourceGroup", sourceGroupEnabled,
> sourceGroupEnabledWithDecoders);
> }
>
> /// <summary>
> /// Method should be called from within a record block and
> /// will setup results for standard things needed during testing
> /// </summary>
> /// <param name="sourceGroupName">The value returned by
> /// DynamiteShooterStateMachine.SourceGroup.Name which
> /// will be used to make the name of the thread
> /// that the test executes on amoung other things</param>
> /// <param name="sourceGroupEnabled">The value returned by
> /// DynamiteShooterStateMachine.SourceGroup.Enabled</param>
> /// <param name="sourceGroupEnabledWithDecoders">The value
> returned by
> /// DynamiteShooterStateMachine.SourceGroup.EnabledWithDecoders</
> param>
> protected void SetupResults(string sourceGroupName, bool
> sourceGroupEnabled,
> bool sourceGroupEnabledWithDecoders)
> {
> if (sourceGroupEnabled == false &&
> sourceGroupEnabledWithDecoders == true)
> throw new InvalidOperationException("Error:
> sourceGroupEnabled can't be false"
> + " if sourceGroupEnabledWithDecoders is true");
>
> SetupResult.For(_sourceGroup.Name).Return(sourceGroupName);
>
> SetupResult.For(_sourceGroup.Enabled).Return(sourceGroupEnabled);
> SetupResult.For(_sourceGroup.EnabledWithDecoders)
> .Return(sourceGroupEnabledWithDecoders);
> }
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---