-1

AdvHtmlHelperという新しいヘルパーを作成しました。

class AdvHtmlHelper extends AppHelper {

    var $helpers = array('Form');

    function textbox($fieldName, $options = array()) {
        $output = $this->Form->input($fieldName, array('before' => '<div class="outerdiv"><div class="leftfields"><div class="txt1">', 'between' => '</div><div class="colon"> : </div></div><div class="rightfields"><div class="input">'));
        $output .= '</div></div></div><div class="space"></div>';
        return $output;
    }
}

そして私はそれのためのテストを作成しました

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->advHtml = new AdvHtmlHelper();
        $this->advHtml->Form = new FormHelper();
        $this->advHtml->Form->Html = new HtmlHelper();
        $this->view = new View($this->Controller);
    }

    //testing textbox() function.
    public function testTextbox() {
        $result = '<div class="input text"><div class="outerdiv"><div class="leftfields"><div class="txt1"><label for="new">New</label></div><div class="colon"> : </div></div><div class="rightfields"><div class="input"><input name="data[new]" type="text" id="new" /></div></div></div></div><div class="space"></div>';
        $this->assertEqual($result, $this->advHtml->textbox('new'));
    }
}

テストを実行しようとすると、次のエラーが発生します。ヘルパーコードの10行目は、フォームヘルパーへの呼び出しです。

致命的なエラー:/opt/lampp/htdocs/mali/app/views/helpers/adv_html.php内の非オブジェクトでメンバー関数input()を呼び出す

別のヘルパーを呼び出すヘルパーをテストするにはどうすればよいですか?

10行目

編集:答えた。参考までに、最終的なテストケースで更新しました。

4

1 に答える 1

3

ヘルパーを設定するときは、フォームヘルパーをadvHtmlヘルパーのプロパティとして設定する必要があります。

public function startTest() {
    $this->advHtml = new AdvHtmlHelper();
    $this->advHtml->Form = new FormHelper();
}
于 2010-12-22T10:30:25.170 に答える