You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 20, 2023. It is now read-only.
<?php
class SomeClass
{
public function doSomething()
{
// Do something
}
public function method()
{
// Do something.
}
}
class StubTest extends PHPUnit_Framework_TestCase
{
public function testStub()
{
// Create a stub for the SomeClass class.
$stub = $this->getMockBuilder('SomeClass')
->getMock();
// Configure the stub.
$stub->method('doSomething')
->willReturn('foo');
// Calling $stub->doSomething() will now return
// 'foo'.
$this->assertEquals('foo', $stub->doSomething());
}
}
output:
[vagrant@localhost tests]$ phpunit Examples/MockExample.php
PHPUnit 4.7.7 by Sebastian Bergmann and contributors.
PHP Fatal error: Call to a member function willReturn() on a non-object in /vagrant/develop/vendor/hades/framework/tests/Examples/MockExample.php on line 26
Fatal error: Call to a member function willReturn() on a non-object in /vagrant/develop/vendor/hades/framework/tests/Examples/MockExample.php on line 26
What Can I do if my class has a method named "method" ?
The text was updated successfully, but these errors were encountered:
The method() method is just a shortcut that was introduced with #147. When the original object already has a method named method then the mock object API's method() is not added and you have to use the non-shortcut version of the API.
I don't understand how to solve my issue from that post #147
Is My Code change like that :
<?php
class SomeClass
{
public function doSomething()
{
// Do something
}
public function method()
{
// Do something.
}
}
class StubTest extends PHPUnit_Framework_TestCase
{
public function testStub()
{
// Create a stub for the SomeClass class.
$stub = $this->getMockBuilder('SomeClass')
->getMock();
// Configure the stub.
$stub->method('doSomething');
PHPUnit_Framework_TestCase::returnValue('foo');
// ->willReturn('foo');
// Calling $stub->doSomething() will now return
// 'foo'.
$this->assertEquals('foo', $stub->doSomething());
}
}
the code like that :
output:
What Can I do if my class has a method named "method" ?
The text was updated successfully, but these errors were encountered: