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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:05:02+00:00 2026-05-11T12:05:02+00:00

I currently have a method within my class that has to call other methods,

  • 0

I currently have a method within my class that has to call other methods, some in the same object and others from other objects.

  class MyClass {     public function myMethod()     {         $var1 = $this->otherMethod1();         $var2 = $this->otherMethod2();         $var3 = $this->otherMethod3();         $otherObject = new OtherClass();         $var4 = $otherObject->someMethod();          # some processing goes on with these 4 variables         # then the method returns something else          return $var5;     } }  

I’m new to the whole TDD game, but some of what I think I understood to be key premises to more testable code are composition, loose coupling, with some strategy for Dependency Injection/Inversion of Control.

How do I go about refactoring a method into something more testable in this particular situation?

Do I pass the $this object reference to the method as a parameter, so that I can easily mock/stub the collaborating methods? Is this recommended or is it going overboard?

  class MyClass {     public function myMethod($self, $other)     {         # $self == $this         $var1 = $self->otherMethod1();         $var2 = $self->otherMethod2();         $var3 = $self->otherMethod3();         $var4 = $other->someMethod();          # ...          return $var5;     } }  

Also, it is obvious to me that dependencies are a pretty big deal with TDD, as one has to think about how to inject a stub/mock to the said method for tests. Do most TDDers use DI/IoC as a primary strategy for public dependencies? at which point does it become exaggerated? can you give some pointers to do this efficiently?

  • 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-11T12:05:02+00:00Added an answer on May 11, 2026 at 12:05 pm

    These are some good questions… let me first say that I do not really know JS at all, but I am a unit tester and have dealt with these issues. I first want to point out that JsUnit exists if you are not using it.

    I wouldn’t worry too much about your method calling other methods within the same class… this is bound to happen. What worries me more is the creation of the other object, depending on how complicated it is.

    For example, if you are instantiating a class that does all kinds of operations over the network, that is too heavy for a simple unit test. What you would prefer to do is mock out the dependency on that class so that you can have the object produce the result you would expect to receive from its operations on the network, without incurring the overhead of going on the network: network failures, time, etc…

    Passing in the other object is a bit messy. What people typically do is have a factory method to instantiate the other object. The factory method can decide, based on whether or not you are testing (typically via a flag) whether or not to instantiate the real object or the mock. In fact, you may want to make the other object a member of you class, and within the constructor, call the factory, or make the decision right there whether or not to instantiate the mock or the real thing. Within the setup function or within your test cases you can set special conditions on the mock object so that it will return the proper value.

    Also, just make sure you have tests for your other functions in the same class… I hope this helps!

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

Sidebar

Related Questions

Currently i have this method: static boolean checkDecimalPlaces(double d, int decimalPlaces){ if (d==0) return
Currently, I don't really have a good method of debugging JavaScript in Internet Explorer and
I currently have an MS Access application that connects to a PostgreSQL database via
I have the UIApplicationDelegate protocol in my main AppDelegate.m class, with the applicationDidBecomeActive method
I am using in-app purchase for an iPhone app. I have a class that
Problem, there's no method: bool ChangePassword(string newPassword); You have to know the current password
I currently have speakers set up both in my office and in my living
I currently have an existing database and I am using the LINQtoSQL generator tool
We currently have a company email server with Exchange, and a bulk email processing
I currently have a fairly robust server-side validation system in place, but I'm looking

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.