का उपयोग करके अमूर्त वर्ग में कंक्रीट विधि का मजाक कर रहे हैं क्या PHPUnit का उपयोग करके अमूर्त कक्षाओं में ठोस तरीकों का नकल करने के कोई अच्छे तरीके हैं?phpunit
मैं अब तक है क्या पाया है:
- उम्मीद() ->() ठीक सार तरीकों
- यह ठोस तरीकों के लिए काम नहीं करता है का उपयोग कर काम करता है जाएगा। मूल विधि इसके बजाए चलाया जाता है।
- मॉकबिल्डर का उपयोग करना और सभी अमूर्त विधियों और सेट विधिओं() कार्यों को कंक्रीट विधि देना। हालांकि, यह आपको सभी अमूर्त तरीकों को निर्दिष्ट करने की आवश्यकता है, परीक्षण को नाजुक और बहुत वर्बोज़ बनाना।
- मॉकबिल्डर :: getMockForAbstractClass() setMethod() को अनदेखा करता है।
यहाँ ऊपर अंक examplifying कुछ इकाई परीक्षण कर रहे हैं:
abstract class AbstractClass {
public function concreteMethod() {
return $this->abstractMethod();
}
public abstract function abstractMethod();
}
class AbstractClassTest extends PHPUnit_Framework_TestCase {
/**
* This works for abstract methods.
*/
public function testAbstractMethod() {
$stub = $this->getMockForAbstractClass('AbstractClass');
$stub->expects($this->any())
->method('abstractMethod')
->will($this->returnValue(2));
$this->assertSame(2, $stub->concreteMethod()); // Succeeds
}
/**
* Ideally, I would like this to work for concrete methods too.
*/
public function testConcreteMethod() {
$stub = $this->getMockForAbstractClass('AbstractClass');
$stub->expects($this->any())
->method('concreteMethod')
->will($this->returnValue(2));
$this->assertSame(2, $stub->concreteMethod()); // Fails, concreteMethod returns NULL
}
/**
* One way to mock the concrete method, is to use the mock builder,
* and set the methods to mock.
*
* The downside of doing it this way, is that all abstract methods
* must be specified in the setMethods() call. If you add a new abstract
* method, all your existing unit tests will fail.
*/
public function testConcreteMethod__mockBuilder_getMock() {
$stub = $this->getMockBuilder('AbstractClass')
->setMethods(array('concreteMethod', 'abstractMethod'))
->getMock();
$stub->expects($this->any())
->method('concreteMethod')
->will($this->returnValue(2));
$this->assertSame(2, $stub->concreteMethod()); // Succeeds
}
/**
* Similar to above, but using getMockForAbstractClass().
* Apparently, setMethods() is ignored by getMockForAbstractClass()
*/
public function testConcreteMethod__mockBuilder_getMockForAbstractClass() {
$stub = $this->getMockBuilder('AbstractClass')
->setMethods(array('concreteMethod'))
->getMockForAbstractClass();
$stub->expects($this->any())
->method('concreteMethod')
->will($this->returnValue(2));
$this->assertSame(2, $stub->concreteMethod()); // Fails, concreteMethod returns NULL
}
}
आप सार वर्गों का परीक्षण करने के रूप में वे सार हैं की जरूरत नहीं है:
कोड देखें। या आप एक सार टेस्टकेस भी लिखना चाहते हैं? – hakre
अमूर्त वर्ग एक और वर्ग की निर्भरता है। इसलिए मैं कुछ क्लास :: getMyCalculatedValue() का परीक्षण करना चाहता हूं जो $ object-> concreteMethod() का उपयोग करता है। चूंकि concreteMethod() बदल सकता है या स्थापित करना मुश्किल हो सकता है, इसलिए मैं concreteMethod() के वापसी मान निर्दिष्ट करना चाहता हूं। – CheeseSucker