Using a method that does nothing, but whose behavior you can configure later. Although I'm not sure that you fully understood how mocking the work is. You should not scoff at the class you are testing, you must scoff at the objects that the test class relies on. For instance:
// class I want to test class TaxCalculator { public function calculateSalesTax(Product $product) { $price = $product->getPrice(); return $price / 5; // whatever calculation } } // class I need to mock for testing purposes class Product { public function getPrice() { // connect to the database, read the product and return the price } } // test class TaxCalculatorTest extends \PHPUnit_Framework_TestCase { public function testCalculateSalesTax() { // since I want to test the logic inside the calculateSalesTax method // I mock a product and configure the methods to return some predefined // values that will allow me to check that everything is okay $mock = $this->getMock('Product'); $mock->method('getPrice') ->willReturn(10); $taxCalculator = new TaxCalculator(); $this->assertEquals(2, $taxCalculator->calculateSalesTax($mock)); } }
Your test makes fun of the exact class you are trying to verify, which might be a mistake, as some methods may be overridden during ridicule.
source share