0

If I have a form like:

class MyForm(modelForm):
    # Form stuff

And a formset like:

MyFormSet = modelformset_factory(
    MyModel,
    form=MyForm,
    max_num=6,
    validate_max=True,
)

Is there a way to test that form=MyForm?

def test_formset(self):

    formset = MyFormSet()

    self.assertEqual(formset.max_num, 6)
    self.assertTrue(formset.validate_max)
    # Tried this but it didn't work
    self.assertIsInstance(formset.form, MyForm)
Carl Brubaker
  • 1,602
  • 11
  • 26

1 Answers1

1

In this case, formset.form will be a subclass of MyForm class, not instance of it, so assertIsInstance will not work. You can check it simply using:

def test_formset(self):

    formset = MyFormSet()
    self.assertTrue(issubclass(formset.form, MyForm))

for formset_factory and when you don't want to allow any subclass of MyForm to be provided, this will also work (as formset_factory won't modify your form class):

def test_formset(self):

    formset = MyFormSet()
    self.assertEquals(formset.form, MyForm)
GwynBleidD
  • 20,081
  • 5
  • 46
  • 77