How to make a distinction between static methods and instance methods when mocking a class?

Houman

I came across a bug in production, even though it should have been tested by the unit tests.

class Stage2TaskView(MethodView):
    def post(self):
        json_data = json.loads(request.data)
        news_url_string = json_data['news_url_string']
        OpenCalais().generate_tags_for_news(news_url_string) // ?
        return "", 201

This used to be a static:

OpenCalais.generate_tags_for_news(news_url_string)

But then I changed the method and removed the static decorator. But I forgot to change that line to

OpenCalais().generate_tags_for_news(news_url_string)

The test doesn't see it though. How can I test this in future?

@mock.patch('news.opencalais.opencalais.OpenCalais.generate_tags_for_news')
def test_url_stage2_points_to_correct_class(self, mo):
    rv = self.client.post('/worker/stage-2', data=json.dumps({'news_url_string': 'x'}))
    self.assertEqual(rv.status_code, 201)
Michele d'Amico

Autospeccing is your fried! Use autospec=True in patch decorator will check the complete signature:

class A():
    def no_static_method(self):
        pass

with patch(__name__+'.A.no_static_method', autospec=True):
    A.no_static_method()

will raise an exception:

Traceback (most recent call last):
  File "/home/damico/PycharmProjects/mock_import/autospec.py", line 9, in <module>
    A.no_static_method()
TypeError: unbound method no_static_method() must be called with A instance as first argument (got nothing instead)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make a distinction between static methods and instance methods when mocking a class?

From Dev

Mocking static methods with jmockit (1.5) before class

From Dev

Mocking static methods with jmockit (1.5) before class

From Java

Mocking static methods with Mockito

From Dev

When to use static and class methods

From Dev

How to mock Python static methods and class methods

From Dev

Why is there a distinction between privileged and public methods? How to know which to use?

From Dev

Mocking static methods with PowerMock and Mockito

From Dev

How could I differentiate between static methods and instance methods in javascript declaration typescript files from definitelyTyped?

From Dev

Extension Methods vs Instance Methods vs Static Class

From Dev

How to decorate class or static methods

From Dev

How to understand `JavaScript static methods are also not callable when the class is instantiated`

From Dev

Does static methods make a class a hybrid?

From Java

What is the difference between class and instance methods?

From Dev

Instance methods defined between a class and a certain ancestor of it

From Dev

Instance methods defined between a class and a certain ancestor of it

From Dev

What's the difference between class methods and instance methods in Swift?

From Dev

Mocking class to test its methods

From Dev

Mocking class methods with OCMock not working

From Dev

Mocking patched class methods is not working

From Dev

Mocking class methods with OCMock not working

From Dev

Mocking methods from a parent Class

From Dev

partial mocking of methods of concrete class

From Dev

How to have both static and instance methods in Javascript

From Dev

PHP: How to make testable static methods

From Dev

How to refactor a bunch of methods to make them static?

From Dev

How to refactor a bunch of methods to make them static?

From Dev

Difference between Class Attributes, Instance Attributes, and Instance Methods in Python

From Dev

How to Know Static Methods's Class Name

Related Related

  1. 1

    How to make a distinction between static methods and instance methods when mocking a class?

  2. 2

    Mocking static methods with jmockit (1.5) before class

  3. 3

    Mocking static methods with jmockit (1.5) before class

  4. 4

    Mocking static methods with Mockito

  5. 5

    When to use static and class methods

  6. 6

    How to mock Python static methods and class methods

  7. 7

    Why is there a distinction between privileged and public methods? How to know which to use?

  8. 8

    Mocking static methods with PowerMock and Mockito

  9. 9

    How could I differentiate between static methods and instance methods in javascript declaration typescript files from definitelyTyped?

  10. 10

    Extension Methods vs Instance Methods vs Static Class

  11. 11

    How to decorate class or static methods

  12. 12

    How to understand `JavaScript static methods are also not callable when the class is instantiated`

  13. 13

    Does static methods make a class a hybrid?

  14. 14

    What is the difference between class and instance methods?

  15. 15

    Instance methods defined between a class and a certain ancestor of it

  16. 16

    Instance methods defined between a class and a certain ancestor of it

  17. 17

    What's the difference between class methods and instance methods in Swift?

  18. 18

    Mocking class to test its methods

  19. 19

    Mocking class methods with OCMock not working

  20. 20

    Mocking patched class methods is not working

  21. 21

    Mocking class methods with OCMock not working

  22. 22

    Mocking methods from a parent Class

  23. 23

    partial mocking of methods of concrete class

  24. 24

    How to have both static and instance methods in Javascript

  25. 25

    PHP: How to make testable static methods

  26. 26

    How to refactor a bunch of methods to make them static?

  27. 27

    How to refactor a bunch of methods to make them static?

  28. 28

    Difference between Class Attributes, Instance Attributes, and Instance Methods in Python

  29. 29

    How to Know Static Methods's Class Name

HotTag

Archive