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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:08:32+00:00 2026-06-18T19:08:32+00:00

My question is about unit testing. Assume we have the class below; class X

  • 0

My question is about unit testing. Assume we have the class below;

class X
{
    public function p1(){
       //logic
       $a = $this->p2();
       //more logic
    }

    public function p2(){
       //even more logic
    }
}

When writing a unit test for p1 method, should I mock p2 method?

What I am thinking is that, the test that is written for p1 method should only execute and test the p1 method not p2. But in order to realize that I should get a mock of Class X and call p1 method on that mock instance like below.

$xMock = $this->getMockBuilder('\X')
    ->setMethods(array('p2'))
    ->getMock();

$xMock->expects($this->any())
    ->method('p2')
    ->will($this->returnValue($value));

$resultTobeAsserted = $xMock->p1();

Unfortunately doing that feels a little wrongish to me. I discussed the topic with my colleagues and it boiled down to how you define your SUT(system under test).
If a tester considers the particular method that is being tested as the SUT, then other methods that are called from the SUT would seem as dependencies and naturally tester will want to mock them. On the other hand if tester considers the whole class as the SUT, then those method calls will become part of the test so there won’t be any reason to mock them.

Is that conclusion correct? Which kind of thinking would yield more robust unit tests?

  • 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-06-18T19:08:34+00:00Added an answer on June 18, 2026 at 7:08 pm

    When writing a unit test for p1 method, should I mock p2 method?

    No.

    You are calling a method on a class and you expects that things happen.

    With mocking p2 you make exceptions on the implementation details on the class.

    Unfortunately doing that feels a little wrongish to me.

    I say the felling it spot on.

    Which kind of thinking would yield more robust unit tests?

    If you test the observable behaviors of a class you make sure that class still does what it was supposed to do when you change the implementation of the class. Thats robustness.

    if you test one method and mock out part of the implementation of that method (the internal method call) then you test a specif implementation and if the test fails you don’t know if the external behavior changed.


    I’ve written about this in some more detail in:

    The UNIT in unit testing.

    which details a couple more points about why i think it’s important to test behaviors and not methods.

    In short

    Unit testing, in PHP, is about testing the observable behaviors of a class!

    Behaviors:

    • return values
    • calling other methods
    • modifying global state (writing to files, the db, $GLOBALS)

    Test those things. Disregard implementation details.

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

Sidebar

Related Questions

I have a question about unit testing. Say I have a controller with one
This question is about the extent to which it makes sense unit testing. I’ve
My question is about unit testing javascript code using Mocha. Ultimately this may be
This question isn't about unit-testing. And it is for a desktop product. This is
A question about how to use test-mvc for unit testing. I have a simple
Unit testing Abstract classes in Groovy I asked a question previous about unit testing
I understand the general idea about unit testing and have used it in scenarios
I am really starting to enjoy unit testing and have the following question to
I have a question about precompiling ASP.NET web application projects from TeamCity. This is
To the question Am I unit testing or integration testing? I have answered, a

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.