admin管理员组

文章数量:1391943

In odoo 17 I need to use unitest.mock to mock the return value of a function The function is called cfdi_cancel. It is located in mymodulename module, inside that module is the 'models' folder, inside that folder is the 'pac_utils' python file, and inside that file is the cfdi_cancel function I am getting the following error

  File "/usr/lib/python3.10/unittest/mock.py", line 1257, in _importer
    thing = __import__(import_path)
ModuleNotFoundError: No module named 'mymodulename'

So, the question is: How can I use unittest.mock to set a return value for that function?

This is my code

from unittest.mock import patch, MagicMock
class TestViixooCFDICancelPayslip(TestPayroll):
    
    @classmethod
    def setUpClass(cls):
        super(TestViixooCFDICancelPayslip, cls).setUpClass()
        cls.payslip.cfdi_uuid = "12345-UUID"
        cls.payslip.cfdi_state = "sent"
        cls.payslip.cfdi_substituted_payslip_id = None

    @patch("mymodulename.models.pac_utils.cfdi_cancel")  # Mock the external cancellation function
    def test_cfdi_cancel_payslip_success(self, mock_cfdi_cancel):
        mock_cfdi_cancel.return_value = {}
        
        self.payslip.cfdi_cancel_payslip("01")
        
        mock_cfdi_cancel.assert_called_once_with(
            context=self.payslip,
            uuid="12345-UUID",
            cancel_reason="01",
            cancel_uuid=None,
        )
        self.assertEqual(self.payslip.cfdi_state, "cancelled")

本文标签: Using unittestMock in Odoo 17Stack Overflow