Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5937689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:34:40+00:00 2026-05-22T15:34:40+00:00

Is this the correct approach when creating a mock which should track the order

  • 0

Is this the correct approach when creating a mock which should track the order in which methods are called for a given class?

class MockFoo():

    def __init__(self):
        self.calledMethods = []

    def medthod1(self):
        self.calledMethods.append(self.medthod1.__name__)

    def medthod2(self):
        self.calledMethods.append(self.medthod2.__name__)

self.assertEqual(len(foo.calledMethods),2)
self.assertEqual(foo.calledMethods[0],MockFoo.title.__name__)

Can I save something better than __name__?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:34:41+00:00Added an answer on May 22, 2026 at 3:34 pm

    You can certainly do this, but consider the following two approaches and decide which is the more readable:

    % cat mockfoo.py
    #!/usr/bin/env python
    
    import unittest
    
    class MockFoo(object):
    
        def __init__(self):
            self.called_methods = []
    
        def method1(self):
            self.called_methods.append(self.method1.__name__)
    
        def method2(self):
            self.called_methods.append('method2')
    
    
    class TestMockFoo(unittest.TestCase):
    
        def test_list_with_name(self):
            foo = MockFoo()
            foo.method1()
            self.assertEquals(1, len(foo.called_methods))
            self.assertEquals(foo.method1.__name__, foo.called_methods[0])
    
        def test_list_readable(self):
            foo = MockFoo()
            foo.method2()
            self.assertEquals(1, len(foo.called_methods))
            self.assertEquals('method2', foo.called_methods[0])
    
    if __name__ == '__main__':
        unittest.main()
    
    % ./mockfoo.py
    ..
    ----------------------------------------------------------------------
    Ran 2 tests in 0.000s
    
    OK
    

    “Readability counts.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating an Entity model applying the Model first approach. I want that
I am new to programming for the iPhone and this will be my first
I guess this has been asked before here , but I'm still confused about
This question covncerns my lack of understanding of how to use the core data
i have this littel problem with a test app. Current look: <form> <span>1:Question goes
I'm building a MVC application which is a bit more complex than what I
In my app (jruby, rails 3, mongodb) I setup my data access objects (and
I an trying to create a program that will detect a face in a
I was just thinking about how recaptcha is getting harder when I thought about
I'm using before and after insert triggers to generate ids (primary key) of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.