Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit testing helpers in CakePHP
    primarykey
    data
    text
    <p>I created a new helper called AdvHtmlHelper.</p> <pre><code>class AdvHtmlHelper extends AppHelper { var $helpers = array('Form'); function textbox($fieldName, $options = array()) { $output = $this-&gt;Form-&gt;input($fieldName, array('before' =&gt; '&lt;div class="outerdiv"&gt;&lt;div class="leftfields"&gt;&lt;div class="txt1"&gt;', 'between' =&gt; '&lt;/div&gt;&lt;div class="colon"&gt; : &lt;/div&gt;&lt;/div&gt;&lt;div class="rightfields"&gt;&lt;div class="input"&gt;')); $output .= '&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="space"&gt;&lt;/div&gt;'; return $output; } } </code></pre> <p>And I created a test for it </p> <pre><code>App::import('Helper', 'AdvHtml'); App::import('Helper', 'Form'); App::import('Helper', 'Html'); App::import('Core', 'View'); class AdvHtmlTest extends CakeTestCase { private $advHtml = null; //Here we instantiate our helper, and all other helpers we need. public function startTest() { $this-&gt;advHtml = new AdvHtmlHelper(); $this-&gt;advHtml-&gt;Form = new FormHelper(); $this-&gt;advHtml-&gt;Form-&gt;Html = new HtmlHelper(); $this-&gt;view = new View($this-&gt;Controller); } //testing textbox() function. public function testTextbox() { $result = '&lt;div class="input text"&gt;&lt;div class="outerdiv"&gt;&lt;div class="leftfields"&gt;&lt;div class="txt1"&gt;&lt;label for="new"&gt;New&lt;/label&gt;&lt;/div&gt;&lt;div class="colon"&gt; : &lt;/div&gt;&lt;/div&gt;&lt;div class="rightfields"&gt;&lt;div class="input"&gt;&lt;input name="data[new]" type="text" id="new" /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="space"&gt;&lt;/div&gt;'; $this-&gt;assertEqual($result, $this-&gt;advHtml-&gt;textbox('new')); } } </code></pre> <p>I get the following error when I try to run the test. Line 10 of the helper code is the call to the form helper.</p> <blockquote> <p>Fatal error: Call to a member function input() on a non-object in /opt/lampp/htdocs/mali/app/views/helpers/adv_html.php</p> </blockquote> <p>How do I test a helper which calls another helper?</p> <blockquote> <p>on line 10</p> </blockquote> <p><strong>EDIT</strong>: Answered. Updated with my final test case for reference.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload