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 5986535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:41:07+00:00 2026-05-22T22:41:07+00:00

I have a method that calls two other methods in it. def main_method(self, query):

  • 0

I have a method that calls two other methods in it.

def main_method(self, query):
  result = self.method_one(query)
  count = self.method_two(result)
  return count

def method_one(self, query):
  #Do some stuff based on results.
  #This method hits the database.
  return result

def method_two(self, result):
  #Do some stuff based on result.
  #This method also hits the database.
  return count

I’m not very experienced at unit testing and have never worked with Mocks and Stubs.

I’m not too sure how to create a unit test for my first method. Since method_one and method_two hit the database many times and they are very expensive, I have decided to use mox to create a mock or stub in order to eliminate the need of hitting database.

I would really appreciate it if someone who has experience working with Mocks and Stubs give me some hints on using mocks and stubs for my case.

  • 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-22T22:41:08+00:00Added an answer on May 22, 2026 at 10:41 pm

    Before worrying about testing main_method(), first test the smaller methods. Consider method_one(). For the purpose of discussion, let’s say it exists in a class like this:

    class Foo(object):
        def method_one(self, query):
            # Big nasty query that hits the database really hard!!
            return query.all()
    

    In order to test that method without hitting the database, we need an object that knows how to respond to the all() method. For example:

    class MockQuery(object):
        def all(self):
            return [1,2]
    

    Now we can test it:

    f = Foo()
    q = MockQuery()
    assert f.method_one(q) == [1,2]
    

    That’s a basic illustration. The real world is often more complicated. In order to be worth the trouble of writing the test, your mock all() would likely do something more interesting than return a constant. Along similar lines, if method_one() contains a bunch of other logic, our MockQuery might need to be more elaborate — that is, capable of responding appropriately to more methods. Often while trying to test code you realize that your original design was overburdened: you might need to refactor method_one() into smaller, more tightly defined — and thus more testable — parts.

    Taking the same logic a step up in the hierarchy, you might create a MockFoo class that would know how to respond in simplified ways to method_one() and method_two().

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

Sidebar

Related Questions

I have a helper method that calls two other helper methods, the problem is
I have a method that calls a service to retrieve an instance of an
If I have a method that calls itself under a certain condition, is it
I have a service method that calls a DAO which then returns an object
I have a method song_link that calls link_to internally. I want the caller to
I have a subclass that calls a method from a superclass. The method in
I have created a console application that calls a method on a webservice. I
Setup: I have a COM DLL that calls a method inside a managed C#
I have a unit test that creates a mock calls my method to be
I have got two methods in application_controller.rb def force_ssl if !request.ssl? redirect_to :protocol =>

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.