简体   繁体   中英

How to fix Assertion error 400 in the following problem?

I am working on Django project. I am writing a test case for the following model:

class Meeting(models.Model):
    team = models.OneToOneField("Team", on_delete=models.CASCADE)
    created_at = models.DateTimeField()

    def __str__(self) -> str:
        return self.team.name

The code for Test case is as follows:

class MeetingTestCase(BaseTestCase):

    def test_meeting_post(self):
        self.client.force_login(self.owner)
        meeting_data = {
            "team": self.team.id,
            "created_at": "2023-01-12T01:52:00+05:00"
        }
        response = self.client.post("/api/meeting/", data=meeting_data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

Inside.setup() function, I have created a team object:

self.team = factories.TeamFactory(name="Team", organization=self.organization)

This is my factory class:

class TeamFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = models.Team

But My test case is failing and I have following assertion error:

self.assertEqual(response.status_code, status.HTTP_201_CREATED)
AssertionError: 400 != 201

which means there is something wrong with my request. What wrong am I doing?

The error was fixed. Field team can have unique values only. I was creating another object in setup() method which was creating same team object, due to which I was getting error that the team field cannot have duplicate values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM