如何进行测试进行现有的COIN_ID?

发布于 2025-02-01 19:14:35 字数 392 浏览 1 评论 0原文

我的代码

有必要替代现有的COIN_ID,因此将来,由于删除了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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘亓 2025-02-08 19:14:35

您应该在测试中创建硬币实例

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文