如何使测试采用现有的 coin_id?
How to make the test take an existing coin_id?
我的代码
需要测试本身可以替代现有的coin_id,这样以后测试就不会掉下来,比如因为id=2[=12下的一个硬币被移除=]
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)
您应该在测试中创建硬币实例
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)
我的代码
需要测试本身可以替代现有的coin_id,这样以后测试就不会掉下来,比如因为id=2[=12下的一个硬币被移除=]
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)
您应该在测试中创建硬币实例
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)