Home > Net >  How to make the test take an existing coin_id?
How to make the test take an existing coin_id?

Time:05-28

My code

It is necessary that the test itself could substitute the existing coin_id, so that in the future the test does not fall, for example, due to the removal of a coin under id=2

class ModelTransactionTest(TestCase):
    @freeze_time(timezone.now())
    def test_auto_setting_date_if_field_empty(self):
        field = Transaction.objects.create(rubles_cost=1, purchase_price=1, amount=1, 
            dollars_cost=1, coin_id=2)
        self.assertEqual(timezone.now(), field.date)

CodePudding user response:

You should create coin instance in tests

class ModelTransactionTest(TestCase):
    def setUp(self) -> None:
        super().setUp()
        self.coin = Coin.objects.create(kwargs required to create object)

    @freeze_time(timezone.now())
    def test_auto_setting_date_if_field_empty(self):
        field = Transaction.objects.create(rubles_cost=1, purchase_price=1, amount=1, 
            dollars_cost=1, coin=self.coin)
        self.assertEqual(timezone.now(), field.date)
  • Related