使用 Mox 模拟 Django 查询

发布于 2024-10-31 14:42:53 字数 1183 浏览 1 评论 0原文

我正在尝试使用 Mox 模拟 django 过滤器查询。我正在按照 Mox 网站上的说明进行操作,但是,由于我的 django 查询是一个链式方法,它会抱怨 AndReturn() 方法不存在。

这是我的方法:

def CheckNameUniqueness(device):
    ex_device = device.__class__.objects.filter(name__iexact=device.name)
    if not ex_device:
        return None
    if ex_device.count() > 0:
        return ex_device

在我的单元测试中,我尝试模拟过滤器方法以返回空列表。

class testCheckNameUniqeness(unittest.TestCase):
    """ Unit test for CheckNameUniqueness function """

    def setUp(self):
        self.device_mocker = mox.Mox()

    def testCheckNameUniqenessNotExists(self):

        device = self.device_mocker.CreateMock(models.Device)
        device.name = "some name"
        device.objects.filter(name__iexact=device.name).AndReturn(None)

        # Put all mocks created by mox into replay mode
        self.device_mocker.ReplayAll()

        # Run the test
        ret = CheckNameUniqueness(device)
        self.device_mocker.VerifyAll()
        self.assertEqual(None, ret)

当我运行测试用例时,出现以下错误: AttributeError: 'QuerySet' object has no attribute 'AndReturn'

请注意,由于大量数据库表、oracle 数据库和其他复杂情况,此单元测试必须在不创建数据库的情况下运行。

I'm trying to mock a django filter query using Mox. I am following the instructions on Mox website, however, since my django query is a chained method, it complains that the AndReturn() method doesn't exist.

Here is my method:

def CheckNameUniqueness(device):
    ex_device = device.__class__.objects.filter(name__iexact=device.name)
    if not ex_device:
        return None
    if ex_device.count() > 0:
        return ex_device

In my unit test, I'm trying to mock the filter method to return an empty list.

class testCheckNameUniqeness(unittest.TestCase):
    """ Unit test for CheckNameUniqueness function """

    def setUp(self):
        self.device_mocker = mox.Mox()

    def testCheckNameUniqenessNotExists(self):

        device = self.device_mocker.CreateMock(models.Device)
        device.name = "some name"
        device.objects.filter(name__iexact=device.name).AndReturn(None)

        # Put all mocks created by mox into replay mode
        self.device_mocker.ReplayAll()

        # Run the test
        ret = CheckNameUniqueness(device)
        self.device_mocker.VerifyAll()
        self.assertEqual(None, ret)

When I run my test case, I get the following error:
AttributeError: 'QuerySet' object has no attribute 'AndReturn'

Note that, because of the large number of database tables, oracle database, and other complications, this unit test has to be run without creating database.

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

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

发布评论

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

评论(2

她比我温柔 2024-11-07 14:42:53

难道不是吗

device.CheckNameUniqueness().AndReturn(None) 

?这就是我阅读 Mox 文档的方式。不过我自己还没有真正使用过它。

Wouldn't it be

device.CheckNameUniqueness().AndReturn(None) 

? That's how I read the Mox documentation. I haven't actually used it myself yet though.

完美的未来在梦里 2024-11-07 14:42:53

我遇到了同样的问题。

def testCheckNameUniqenessNotExists(self):
    self.device_mocker.StubOutWithMock(models.Device, "objects")

    models.Device.objects.filter(name__iexact=device.name).AndReturn(None)

    # Put all mocks created by mox into replay mode
    self.device_mocker.ReplayAll()

    # Run the test
    ret = CheckNameUniqueness(device)
    self.device_mocker.VerifyAll()
    self.assertEqual(None, ret)

如果你想链接 QuerySet,你可以模拟 QuerySet 并将其作为返回:

from django.db.models.query import QuerySet

def testCheckNameUniqenessNotExists(self):
    qs = self.device_mocker.CreateMock(QuerySet)
    self.device_mocker.StubOutWithMock(models.Device, "objects")

    models.Device.objects.filter(name__iexact=device.name).AndReturn(qs)
    qs.count().AndReturn(1)

    # Put all mocks created by mox into replay mode
    self.device_mocker.ReplayAll()

    # Run the test
    ret = CheckNameUniqueness(device)
    # etc...

I ran into this same problem.

def testCheckNameUniqenessNotExists(self):
    self.device_mocker.StubOutWithMock(models.Device, "objects")

    models.Device.objects.filter(name__iexact=device.name).AndReturn(None)

    # Put all mocks created by mox into replay mode
    self.device_mocker.ReplayAll()

    # Run the test
    ret = CheckNameUniqueness(device)
    self.device_mocker.VerifyAll()
    self.assertEqual(None, ret)

If you want to chain QuerySets, you can make a mock of a QuerySet and have it be the return:

from django.db.models.query import QuerySet

def testCheckNameUniqenessNotExists(self):
    qs = self.device_mocker.CreateMock(QuerySet)
    self.device_mocker.StubOutWithMock(models.Device, "objects")

    models.Device.objects.filter(name__iexact=device.name).AndReturn(qs)
    qs.count().AndReturn(1)

    # Put all mocks created by mox into replay mode
    self.device_mocker.ReplayAll()

    # Run the test
    ret = CheckNameUniqueness(device)
    # etc...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文