Unit test for CMFFormController validator
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"

Author: