Expectation is not sticking and when debugging code steps into expected method

2 views
Skip to first unread message

Ryan Vice

unread,
Oct 3, 2008, 12:29:12 PM10/3/08
to Rhino.Mocks
I'm attempting to set an expectation on a method

Expect.Call(delegate

{ _dynamiteShooterSm.CallInitializeState(_dynamiteShooterSm.qUnableToShoot); });

and when I run my code I expect that the first call on that method
should either throw an exception if the parameters don't match or it
should step over that method and continue executing (method returns
void). This is not happening, in the debugger the code steps into the
method that I set the expecation on. Full code is below:

----------------------------------------
test code
----------------------------------------

/// <summary>
/// Class for testing
DynamiteShooterStateMachine.InitializeStateMachine()
/// </summary>
public class InitializeStateMachine :
DynamiteShooterStateMachineTestsBase
{
#region Tests

///////////////////////////////////////////////
// NAMING FORMAT KEY
// In this section the name of the tests if formatted
// as <condition>_<result> where:
// <condition> is the test condtion
// <result> is the expected state
///////////////////////////////////////////////

[Test]
public void DecoderDisabled_UnableToShoot()
{
using (_mockRepository.Record())
{
SetupResults();
SetupResult.For(_sourceGroup.Enabled).Return(false);
Expect.Call(delegate

{ _dynamiteShooterSm.CallInitializeState(_dynamiteShooterSm.qUnableToShoot); }); //
====> this is the expectation that doesn't stick <========
}

using (_mockRepository.Playback())
{
_dynamiteShooterSm.Init();
}
}

#endregion // Tests
}

#endregion // InitializeStateMachine

#region DynamiteShooterStateMachineTestsBase

/// <summary>
/// Base class that initializes testing infrastructure for
/// DynamiteShooterStateMachine
/// </summary>
[TestFixture]
public class DynamiteShooterStateMachineTestsBase
{
#region Member Varibles and Properties

internal DynamiteShooterStateMachineTestHarness
_dynamiteShooterSm;
protected MockRepository _mockRepository;
protected ISourceGroup _sourceGroup;
protected ISourcePoint _sourcePoint;

#endregion

#region Setup

[SetUp]
public void Setup()
{
_mockRepository = new MockRepository();
_sourceGroup = _mockRepository.DynamicMock<ISourceGroup>();
_sourcePoint = _mockRepository.DynamicMock<ISourcePoint>();
_dynamiteShooterSm =

_mockRepository.PartialMock<DynamiteShooterStateMachineTestHarness>
(_sourceGroup,
_sourcePoint);
}

/// <summary>
/// Method should be called from within a record block and
/// will setup results for standard things needed during testing
/// </summary>
protected void SetupResults()
{
SetupResults("TestSourceGroup");
}

/// <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>
protected void SetupResults(string sourceGroupName)
{
SetupResult.For(_sourceGroup.Name).Return(sourceGroupName);
}

#endregion // Setup

}

#endregion // DynamiteShooterStateMachineTestsBase

#region DynamiteShooterStateMachineTestHarness

/// <summary>
/// A testing harness that provides access to protected\internal
/// members of DynamiteShooterStateMachine
/// </summary>
internal class DynamiteShooterStateMachineTestHarness :
DynamiteShooterStateMachine
{
#region Members variables and properties

#region Testing accessors

//////////////////////////////////////////////////////////
// Testing get accessors
// THESE SHOULD ONLY BE USED IN TESTING CODE!
// note: q prefix was used to group testing states in
// intellisense
//////////////////////////////////////////////////////////

internal QState qUnableToShoot { get { return
_unableToShoot; } }
internal QState qAbleToShoot { get { return _ableToShoot; } }
internal QState qNotReady { get { return _notReady; } }
internal QState qBadReadyToArm { get { return
_badReadyToArm; } }
internal QState qBadReadyToFire { get { return
_badReadyToFire; } }
internal QState qReadyToArm { get { return _readyToArm; } }
internal QState qWaitingForReadyToFire { get { return
_waitingForReadyToFire; } }
internal QState qReadyToFire { get { return _readyToFire; } }
internal QState qShooting { get { return _shooting; } }
internal QState qStartCodeSubmitted { get { return
_startCodeSubmitted; } }
internal QState qSweeping { get { return _sweeping; } }
internal QState qListening { get { return _listening; } }
internal QState qAcquired { get { return _acquired; } }


#endregion // Testing accessors

#endregion // Members variables and properties

#region Constructors

/// <summary>
/// Constructor for this class.
/// </summary>
/// <param name="sourceGroup">The assigned source group for this
broker.</param>
internal DynamiteShooterStateMachineTestHarness(ISourceGroup
sourceGroup)
: base(sourceGroup, null) {}

/// <summary>
/// Constructor for this class.
/// </summary>
/// <param name="sourceGroup">The assigned source group for this
broker.</param>
/// <param name="sourcePoint">The assigned source point at which
this broker is located.</param>
internal DynamiteShooterStateMachineTestHarness(ISourceGroup
sourceGroup, ISourcePoint sourcePoint)
: base(sourceGroup, sourcePoint) {}

#endregion // Constructors
}


----------------------------------------
method under test code
----------------------------------------

protected override void InitializeStateMachine()
{
if (string.IsNullOrEmpty(Thread.CurrentThread.Name))
{
Thread.CurrentThread.Name = _sourceGroup.Name + " dynamite
state machine";
}

// Transition to the correct initial state based on the
existence of a source point.
if (_sourceGroup.Enabled == false ||
_sourceGroup.EnabledWithDecoders == false)
CallInitializeState(_unableToShoot); // initial transition
else if (_sourcePoint == null)
CallInitializeState(_notReady);
else
CallInitializeState(_ableToShoot);
}

Ryan Vice

unread,
Oct 3, 2008, 12:39:32 PM10/3/08
to Rhino.Mocks
and here is the declaration of the method that is having the
expectation set on it. also note that i've added

// This gives Rhino Mocks acces internals under test
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

to allow internals to be mocked.


-------------------------------------

#region Pass through methods for testing

//////////////////////////////////////////////////////
// THESE METHODS ARE FOR TESTING USE ONLY!
// This section contains methods that allow for using
// the Rhino framework for mocking. You can only
// mock internal\public methods so these
// pass through methods were added to address that need
//////////////////////////////////////////////////////

internal void CallInitializeState(QState state)
{
InitializeState(state);
}

internal void CallTransitionTo(QState state)
{
TransitionTo(state);
}

#endregion // Pass through methods for testing

Ayende Rahien

unread,
Oct 3, 2008, 2:38:18 PM10/3/08
to Rhino...@googlegroups.com
It should be protected internal

Ayende Rahien

unread,
Oct 3, 2008, 2:38:26 PM10/3/08
to Rhino...@googlegroups.com
And virtual, of course.
Reply all
Reply to author
Forward
0 new messages