Warning

This document hasn't been checked for compatibility with current versions of Plone. Use at your own risk.

Unit test for CMFFormController validator

by Michael Davis last modified Dec 30, 2008 03:03 PM
How to write a unit test for a CMFFormController validate script

 To unit test a validate script you need to setup a state object to pass to CMFFormController along with the validate script you are testing.

Once you set up the form variables, you pass the form controller template id, and set the context to the form controller template in the dummy_controller_state. Then pass that to CMFFormController along with the validate script you are testing. Then you can run some tests to ensure the validate script has done the right thing.

   def testQuestionValidates(self):
        s1 = getattr(self, 's1')
        app = makerequest(self.app)
        # add your form variables
        app.REQUEST.form['stq1'] = 'Text Answer'
        # set up a dummy state object
        dummy_controller_state = ControllerState(
                                    id='survey_view',
                                    context=s1,
                                    button='submit',
                                    status='success',
                                    errors={},
                                    next_action=None,)
        # get the form controller
        controller = self.portal.portal_form_controller
        # send the validate script to the form controller with the dummy state object
        controller_state = controller.validate(dummy_controller_state, app.REQUEST, ['validate_survey',])
        # Do any relevant tests

assert controller_state.getErrors() == {}, "Validation error raised"
        userid = s1.getSurveyId()
        assert userid == "test_user_1_", "Not default test user"
        questions = s1.getQuestions()
        for question in questions:
            if question.portal_type == 'Survey Text Question':
                question.addAnswer('Text answer')
                assert question.getAnswerFor(userid) == 'Text answer', "Answer not saved correctly"

Contribute

Something wrong or out of date? Anybody can edit or create a new article in the knowledge base. Simply create an account on this site, log in, and click the Edit button to contribute.