0

I tried to test forms.py in Django which has LeadForm class.

Here, is the models.py

class Lead(TimestampedModel):
    """leads for LIVE courses"""

    course = models.ForeignKey(
        LiveCourse, related_name="leads", on_delete=models.PROTECT
    )
    notes = models.TextField(blank=True, null=True)

Here, is the code I used to test.

@pytest.fixture
def valid_course(db):
    return Course.objects.create(name='Design')


@pytest.fixture
def valid_lead_data(valid_course):
    return {
        'full_name': 'john',
        'phone_number': '1234567890',
        'email': '[email protected]',
        'course': valid_course.id,
        'g-recaptcha-response': 'PASSED',
    }

The above code shows AssertionError that the course is not in one of the available choices.

Thanks in advance.

5
  • Is that form related to a model? If it is, Can you provide associated model?
    – Mehdi
    Commented Jun 4 at 6:11
  • Please provide a minimal reproducible example, to begin with you're very clearly missing the failing test. Commented Jun 4 at 6:55
  • Although the cause of your error might be because you're using the wrong model, your foreign key is for LiveCourse while your fixture is creating a Course object. Commented Jun 4 at 6:57
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example.
    – Community Bot
    Commented Jun 4 at 7:13
  • Why don't you use the "TestCaase" in "tests.py" of django? instead of using pytest decorator?
    – Mehdi
    Commented Jun 4 at 7:53

1 Answer 1

0

Note that in valid_course you create Course object, but in the Lead model course field related to LiveCourse model.

Not the answer you're looking for? Browse other questions tagged or ask your own question.