python mock global function that is used in class

Robin van Leeuwen

I can't seem to get my head around mocking in Python. I have a global function:

a.py:

def has_permission(args):
    ret_val = ...get-true-or-false...
    return ret_val

b.py:

class MySerializer(HyperlinkedModelSerializer):

     def get_fields():
         fields = super().get_fields()
         for f in :
             if has_permission(...):
                 ret_val[f.name] = fields[f]
         return ret_val

c.py:

class CountrySerializer(MySerializer):
    class Meta:
        model = Country

Question: Now i want to test c.py, but i want to mock the has_permission function that is defined in a.py, but is called in the get_fields-method of the class MySerializer that is defined in b.py ... How do i do that?

I've tried things like:

@patch('b.MySerializer.has_permission')

and

@patch('b.MySerializer.get_fields.has_permission')

and

@patch('a.has_permission')

But everything i try either just doesn't work and has_permission is still executed, or python complains about that it can't find the attribute 'has_permission'

with the patching done in:

test.py

class TestSerializerFields(TestCase):
    @patch(... the above examples....)
    def test_my_country_serializer():
        s = CountrySerializer()
        self..assertTrue(issubclass(my_serializer_fields.MyCharField, type(s.get_fields()['field1'])))
Martijn Pieters

You need to patch the global in the b module:

@patch('b.has_permission')

because that's where your code looks for it.

Also see the Where to patch section of the mock documentation.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Mock not overriding the return of a function in Python

分類Dev

Python – How to mock a single function

分類Dev

PHP: how to used a class function?

分類Dev

Does python's `unittest.mock.patch` mutate global state?

分類Dev

How to call global function from class method

分類Dev

Global Variable used inside function's if statement seems to be blank JavaScript

分類Dev

Python 3 unittest cannot mock method used inside functions

分類Dev

class lookup function python

分類Dev

How to access global variable from inside class's function

分類Dev

PHP: Static method in Class vs Global function in Namespace?

分類Dev

Python mock builtin 'open' in a class using two different files

分類Dev

How do I mock a class in a Python unit test?

分類Dev

In Python, can a loop be used within a function?

分類Dev

Python NLTK: generate() function cannot be used

分類Dev

Python's print function in a class

分類Dev

Disabling the function in base class in python

分類Dev

Mock interface in the test class

分類Dev

React testing - mock function

分類Dev

Writing a Python class that can only be used as a context manager

分類Dev

how to mock an object that is used inside the tested method?

分類Dev

how to mock a class and override a method

分類Dev

Mock a class for testing with Typescript and Jest

分類Dev

Delete object instance from class function in python

分類Dev

unable to call member function in class python

分類Dev

Passing global pointer to function

分類Dev

Python Mock not asserting calls

分類Dev

Partial patch in Python with mock

分類Dev

Enzyme mock function before componentWillMount

分類Dev

Mock function without callback as parameter

Related 関連記事

ホットタグ

アーカイブ