开发者

Unit testing large non-member functions

I have a function, which I'd like to get under test. However, it's very large and creates a finite state machine. The function looks roughly like:

std::auto_ptr<IFiniteStateMachine> CreateFSM() 
{
    std::auto_ptr<IFiniteStateMachine> = apFSM( new CFiniteStateMachine() );

    std::list<IState*> listStates;

    listState.push_back( new CStateFoo( /*params*/ ) );
    listState.push_back( new CStateBar( /*params*/ ) );

    // etc.

    apFSM->AddState( listStates );

    // Define the tr开发者_开发技巧ansition table
    // 1st param: event received
    // 2nd param: current state
    // 3rd param: next state
    apFSM->AddTransition( "EventA", "StateFoo", "StateBar" );

    // etc.

    return apFSM;
}

Basically, this function just creates a FSM, states, and transition tables. Each of the individual components (the IFiniteStateMachine and IState classes) are testable.

So my question is: can a function like this be put under test? I'm not sure how you would even verify that this function worked as expected. I could probably divide it up into smaller functions that create states and transitions. However, a function that just created these states also seems rather complicated and hard to test.

Thanks.


Ask yourself what should be true after the function has run? Sounds like in this case a certain state machine should be in place - so that's what you should check for so you can do something like:

FSM expectedFSM = Factory.blankFSM().addState().addTransition().... etc
FSM actualFSM = CreateFSM();
ASSERT(expectedFSM == actualFSM); //assuming such == exists

or sometimes an easier way of doing this could be using string represntation/serialization:

String expectedFSMStr = loadFSMFromFile(expectedOutput1);
FSM actualFSM = CreateFSM();
ASSERT_EQUALS(expectedFSMStr, actualFSM.toString());

This can be easier to debug and is very useful if your classes have a reliable toStrinb/serialization method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜