2010-12-04 24 views
5

में यूनिटटेस्ट त्रुटि नियंत्रक Iam 100% कोड कवरेज का प्रशंसक है, लेकिन मुझे नहीं पता कि ज़ेंड फ्रेमवर्क में त्रुटि नियंत्रक का परीक्षण कैसे करें।ज़ेंड फ्रेमवर्क

public function testDispatchErrorAction() 
    { 
     $this->dispatch('/error/error'); 
     $this->assertResponseCode(200); 
     $this->assertController('error'); 
     $this->assertAction('error'); 
    } 

    public function testDispatch404() 
    { 
     $this->dispatch('/error/errorxxxxx'); 
     $this->assertResponseCode(404); 
     $this->assertController('error'); 
     $this->assertAction('error'); 
    } 

लेकिन यह कैसे एक आवेदन त्रुटि (500) के लिए परीक्षण करने के लिए:

यह 404Action और errorAction परीक्षण करने के लिए कोई समस्या नहीं है? शायद मुझे ऐसा कुछ चाहिए?

public function testDispatch500() 
{ 
    throw new Exception('test'); 

    $this->dispatch('/error/error'); 
    $this->assertResponseCode(500); 
    $this->assertController('error'); 
    $this->assertAction('error'); 

} 

उत्तर

0

ठीक है, मैं इस विषय के साथ बहुत परिचित नहीं हूँ, लेकिन मैं एक कस्टम ErrorHandler प्लगइन के साथ इस व्यवहार में हेरफेर चाहते हैं (मूल का विस्तार, और नाटक है कि एक अपवाद फेंका गया था)। शायद इसे केवल एक परीक्षण के लिए पंजीकृत करना संभव है।

1

यह एक पुराना सवाल है लेकिन मैं आज इस के साथ संघर्ष कर रहा था और कहीं और अच्छा जवाब नहीं मिला, इसलिए मैं आगे बढ़ूंगा और पोस्ट करूंगा जो मैंने इस समस्या को हल करने के लिए किया था। जवाब वास्तव में वास्तव में सरल है।

अपने प्रेषण को उस क्रिया में इंगित करें जिसके परिणामस्वरूप एक अपवाद हो जाएगा।

जेएसओएन एंड पॉइंट पर अनुरोध प्राप्त होने पर मेरा एप्लिकेशन एक त्रुटि फेंकता है, इसलिए मैंने इसका परीक्षण करने के लिए उनमें से एक का उपयोग किया।

/** 
    * @covers ErrorController::errorAction 
    */ 
    public function testErrorAction500() { 
     /** 
     * Requesting a page that doesn't exist returns the proper error message 
     */ 
     $this->dispatch('/my-json-controller/json-end-point'); 
     $body = $this->getResponse()->getBody(); 
     $this->assertResponseCode('500'); 
     $this->assertContains('Application error',$body); 
    } 

वैकल्पिक रूप से, अगर आप सिर्फ परीक्षण के लिए एक कार्य होने से परहेज नहीं करते, तो आप सिर्फ एक कार्रवाई है कि केवल अपने इकाई परीक्षण में है कि कार्रवाई करने के लिए एक त्रुटि और बिंदु फेंकता बना सकते हैं।

public function errorAction() { 
    throw new Exception('You should not be here'); 
} 

फिर अपने परीक्षण इस प्रकार दिखाई देगा:

/** 
    * @covers ErrorController::errorAction 
    */ 
    public function testErrorAction500() { 
     /** 
     * Requesting a page that doesn't exist returns the proper error message 
     */ 
     $this->dispatch('/my-error-controller/error'); 
     $body = $this->getResponse()->getBody(); 
     $this->assertResponseCode('500'); 
     $this->assertContains('Application error',$body); 
    } 
संबंधित मुद्दे