2

I must be missing something but I followed this tutorial: http://www.phpunit.de/manual/current/en/test-doubles.html

 <?php
    class SomeClass
    {
      public function doSomething()
      {
         // Do something.
         return 'bar';
      }
    }
 ?>

My StubTest class

class StubTest extends PHPUnit_Framework_TestCase
{
  public function testStub()
  {
    // Create a stub for the SomeClass class.
    $stub = $this->getMock('SomeClass');

    // Configure the stub.
    $stub->expects($this->any())
         ->method('doSomething')
         ->will($this->returnValue('foo'));

    // Calling $stub->doSomething() will now return
   $this->assertEquals('foo', $stub->doSomething());
  }
 }
 ?>

Well maybe I am missing something but isn't it the expected value from invoking doSomething is bar?

If I do $this->assertEquals('bar', $stub->doSomething()); it will fail.

It seem that it is base against ->will($this->returnValue('foo'));

1
  • 2
    Your code seems to be fine. What do you mean "it fails"? $stub->doSomething() returns 'bar' or maybe it return null or test crashes? Which version of phpunit do you use? Commented Oct 5, 2012 at 9:27

1 Answer 1

3

Your test should pass. The main code will return 'bar', but you are not calling the main code. You mocked the object to return 'foo'. Therefore, it should return 'foo' which is what your test is showing.

To simulate the same return from your code with the mock, you would do the following:

$stub = $this->getMock('SomeClass');

// Configure the stub.
$stub->expects($this->any())
     ->method('doSomething')
     ->will($this->returnValue('bar'));

// Calling $stub->doSomething() will now return
$this->assertEquals('bar', $stub->doSomething());

This will allow your test to continue as if you called the real function and received 'bar' as the return.

Sign up to request clarification or add additional context in comments.

4 Comments

See this is the confusing part. It seem assertEqual will test if match with ->will($this->returnValue('bar')); As in this case I think it return foo, so i said should expect return foo. But the actual method return bar shouldn't that fail?
the actual method is never called, that is exactly the purpose of a stub. You never test a stub itself, you use it for the dependencies of your test objects.
As fab indicated, you do not actually call, or use, the original code. The Mock is meant to be a replacement. The Mock is used to allow to return the exact data you want returned for the test. This allows your unit test to work on code when the function returned the value you specified. This allows you to test that your code behaves if 'bar' is returned instead of 'foo'. Another Mock would be used to return 'foo' so you could test it as well.
You would also create unit tests for the object you are mocking (SomeClass) to test that it returns foo appropriately to the conditions presented.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.