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

  • SEARCH
  • Home
  • 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 60983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:14:50+00:00 2026-05-10T18:14:50+00:00

In my question As a mockist TDD practitioner, should I mock other methods in

  • 0

In my question As a “mockist” TDD practitioner, should I mock other methods in the same class as the method under test?, Avdi answered ‘Personally I think that mocking on self is almost always a code smell. It’s testing the implementation rather than the behavior.’ He may be right, but often I can’t distinguish between the implementation and the behavior.

I have another example (in Python-style pseudo-code) that may lead to helpful answers:

class Consumer:      def spec_dirpath:         client = VCS.get_connection(self.vcs_client_name)         client.sync()         return client.dirpath()      def spec_filepath:         filepath = os.path.join(spec_dirpath(), self.spec_filename)         if not os.path.exists(filepath):             raise ConsumerException         return filepath      def get_components:         return Components.get_components_from_spec_file(self.spec_filepath()) 

The idea here is that the get_components method calls the spec_filepath method in order to get a path to a file that the get_components_from_spec_file Components class method will read a list of components from. The spec_filepath method in turn calls spec_dirpath, which syncs the directory containing the spec file from the VCS system and returns the path to that directory. (Try not to look for bugs in this code–it’s pseudo-code, after all.)

I’m looking for advice on how to test these methods…

Testing spec_dirpath should be quite straightforward. I can mock the VCS class and have it return a mock object and confirm the appropriate methods are called (and that the spec_dirpath method returns what the mock’s dirpath method returns).

But if I don’t mock spec_dirpath while testing spec_filepath, how do I avoid duplicating the same test code from the spec_dirpath code in the spec_filepath test? And if I don’t mock spec_filepath while testing get_components, how do I avoid duplicating the test code from both spec_filepath and spec_dirpath?

  • 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. 2026-05-10T18:14:51+00:00Added an answer on May 10, 2026 at 6:14 pm

    Unit testing works better with some form of dependency injection. In this case because you are creating the Client in the code you create a dependency in your test that requires some form of dynamic mock to cope with. (We’d use a partial mock in this case to mock our calls that create dependencies to avoid having to test those dependencies as well).

    If you inject the dependency on startup (i.e. Pass in a client object) then you can mock it easily and not have major difficulties testing just the one class.

    So you need either a partial mock or a dependency injection solution to meet those objectives.

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

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Many times you'll see people use the onclick attribute, and… May 12, 2026 at 1:04 am
  • Editorial Team
    Editorial Team added an answer Actually, it was easier to solve that I thought. Since… May 12, 2026 at 1:04 am
  • Editorial Team
    Editorial Team added an answer Here is an (untested) idea: Grab the name of your… May 12, 2026 at 1:04 am

Related Questions

Edit: Language/Platform is C# / .Net I'm currently trying to fill a vast unit-test
Say I have class A with class A { final String foo() { //
As a glutton for unproven sexy techniques I've adopted System.Web.Routing in my Web Forms
I work as a ERP programmer, mostly with MS SQL and some kind of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.