致命错误:在非对象上调用成员函数create()

时间:2016-02-19 20:20:39

标签: cakephp

我需要在Controller中测试add方法,我正在使用cakephp框架和phpunit进行测试,控制器中的方法如下所示:

public function add() {

            $this->CategoryProblem->create();
            if ($this->CategoryProblem->save($this->request->data)) {
                $message = 'Saved';
            } else {
                $message = 'Error';
            }
            $this->set(array(
            'message' => $message,
            '_serialize' => array('message')
            ));

    }

,测试看起来像这样:

public function testAdd() {
        $data = array(
            'CategoryProblems' => array(
                'id' => '15',
                'parent_id'=>null,
                'modified'=> '',
                'deleted'=>'',
                'name'=>'raspado',
                'description'=>null,

            ),
        );

        $this->testAction('/categoryproblems/add',array('data'=> $data ));
        $result = $this->_testAction('/categoryproblems.json',array('method'=>'pot','data'=>array(json_decode('message'))));
        $this->assertTrue($result,'Saved');

            debug($result);
    }

当我运行测试时出现此错误:

C:\xampp_183\php\php.exe C:\xampp_183\htdocs\MDCW\Projects\services\cakeunit4phpstorm.php C:/Users/yaima.vazquez/AppData/Local/Temp/ide-phpunit.php --cake-case=Controller\CategoryProblemsController --cake-app --no-configuration C:\xampp_183\htdocs\MDCW\Projects\services\app\Test\Case\Controller
Testing started at 2:54 PM ...
<pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr56c772f626e32-trace').style.display = (document.getElementById('cakeErr56c772f626e32-trace').style.display == 'none' ? '' : 'none');"><b>Warning</b> (2)</a>: Cannot modify header information - headers already sent by (output started at C:\xampp_183\php\pear\PHPUnit\Util\Printer.php:172) [<b>APP\Controller\AppController.php</b>, line <b>41</b>]<div id="cakeErr56c772f626e32-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr56c772f626e32-code').style.display = (document.getElementById('cakeErr56c772f626e32-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr56c772f626e32-context').style.display = (document.getElementById('cakeErr56c772f626e32-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr56c772f626e32-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">{</span></span></code>
<code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">set</span><span style="color: #007700">(</span><span style="color: #DD0000">'params'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">Router</span><span style="color: #007700">::</span><span style="color: #0000BB">getParams</span><span style="color: #007700">());</span></span></code>
<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Access-Control-Allow-Origin:*'</span><span style="color: #007700">);</span></span></code></span></pre><pre class="stack-trace">header - [internal], line ??
AppController::beforeFilter() - APP\Controller\AppController.php, line 41
call_user_func - [internal], line ??
CakeEventManager::dispatch() - CORE\Cake\Event\CakeEventManager.php, line 243
Controller::startupProcess() - CORE\Cake\Controller\Controller.php, line 676
Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 189
Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 167
ControllerTestCase::_testAction() - CORE\Cake\TestSuite\ControllerTestCase.php, line 279
ControllerTestCase::__call() - CORE\Cake\TestSuite\ControllerTestCase.php, line 191
CategoryProblemsControllerTest::testAction() - APP\Test\Case\Controller\CategoryProblemsControllerTest.php, line 26
CategoryProblemsControllerTest::testIndex() - APP\Test\Case\Controller\CategoryProblemsControllerTest.php, line 26
ReflectionMethod::invokeArgs() - [internal], line ??
PHPUnit_Framework_TestCase::runTest() - C:\xampp_183\php\pear\PHPUnit\Framework\TestCase.php, line 976
PHPUnit_Framework_TestCase::runBare() - C:\xampp_183\php\pear\PHPUnit\Framework\TestCase.php, line 831
PHPUnit_Framework_TestResult::run() - C:\xampp_183\php\pear\PHPUnit\Framework\TestResult.php, line 648
PHPUnit_Framework_TestCase::run() - C:\xampp_183\php\pear\PHPUnit\Framework\TestCase.php, line 776
CakeTestCase::run() - CORE\Cake\TestSuite\CakeTestCase.php, line 82</pre></div></pre><pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr56c772f627e23-trace').style.display = (document.getElementById('cakeErr56c772f627e23-trace').style.display == 'none' ? '' : 'none');"><b>Warning</b> (2)</a>: Cannot modify header information - headers already sent by (output started at C:\xampp_183\php\pear\PHPUnit\Util\Printer.php:172) [<b>APP\Controller\AppController.php</b>, line <b>42</b>]<div id="cakeErr56c772f627e23-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr56c772f627e23-code').style.display = (document.getElementById('cakeErr56c772f627e23-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr56c772f627e23-context').style.display = (document.getElementById('cakeErr56c772f627e23-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr56c772f627e23-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">set</span><span style="color: #007700">(</span><span style="color: #DD0000">'params'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">Router</span><span style="color: #007700">::</span><span style="color: #0000BB">getParams</span><span style="color: #007700">());</span></span></code>
<code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Access-Control-Allow-Origin:*'</span><span style="color: #007700">);</span></span></code>
<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Access-Control-Allow-Headers:Content-Type&nbsp;,Content-Range,&nbsp;Content-Disposition,&nbsp;Content-Description'</span><span style="color: #007700">);</span></span></code></span></pre><pre class="stack-trace">header - [internal], line ??
AppController::beforeFilter() - APP\Controller\AppController.php, line 42
call_user_func - [internal], line ??
CakeEventManager::dispatch() - CORE\Cake\Event\CakeEventManager.php, line 243
Controller::startupProcess() - CORE\Cake\Controller\Controller.php, line 676
Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 189
Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 167
ControllerTestCase::_testAction() - CORE\Cake\TestSuite\ControllerTestCase.php, line 279
ControllerTestCase::__call() - CORE\Cake\TestSuite\ControllerTestCase.php, line 191
CategoryProblemsControllerTest::testAction() - APP\Test\Case\Controller\CategoryProblemsControllerTest.php, line 26
CategoryProblemsControllerTest::testIndex() - APP\Test\Case\Controller\CategoryProblemsControllerTest.php, line 26
ReflectionMethod::invokeArgs() - [internal], line ??
PHPUnit_Framework_TestCase::runTest() - C:\xampp_183\php\pear\PHPUnit\Framework\TestCase.php, line 976
PHPUnit_Framework_TestCase::runBare() - C:\xampp_183\php\pear\PHPUnit\Framework\TestCase.php, line 831
PHPUnit_Framework_TestResult::run() - C:\xampp_183\php\pear\PHPUnit\Framework\TestResult.php, line 648
PHPUnit_Framework_TestCase::run() - C:\xampp_183\php\pear\PHPUnit\Framework\TestCase.php, line 776
CakeTestCase::run() - CORE\Cake\TestSuite\CakeTestCase.php, line 82</pre></div></pre><pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr56c772f6288ab-trace').style.display = (document.getElementById('cakeErr56c772f6288ab-trace').style.display == 'none' ? '' : 'none');"><b>Warning</b> (2)</a>: Cannot modify header information - headers already sent by (output started at C:\xampp_183\php\pear\PHPUnit\Util\Printer.php:172) [<b>APP\Controller\AppController.php</b>, line <b>43</b>]<div id="cakeErr56c772f6288ab-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr56c772f6288ab-code').style.display = (document.getElementById('cakeErr56c772f6288ab-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr56c772f6288ab-context').style.display = (document.getElementById('cakeErr56c772f6288ab-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr56c772f6288ab-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Access-Control-Allow-Origin:*'</span><span style="color: #007700">);</span></span></code>
<code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Access-Control-Allow-Headers:Content-Type&nbsp;,Content-Range,&nbsp;Content-Disposition,&nbsp;Content-Description'</span><span style="color: #007700">);</span></span></code>
<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Access-Control-Allow-Methods:GET,&nbsp;POST,&nbsp;PUT,&nbsp;DELETE,&nbsp;OPTIONS'</span><span style="color: #007700">);</span></span></code></span></pre><pre class="stack-trace">header - [internal], line ??
AppController::beforeFilter() - APP\Controller\AppController.php, line 43
call_user_func - [internal], line ??
CakeEventManager::dispatch() - CORE\Cake\Event\CakeEventManager.php, line 243
Controller::startupProcess() - CORE\Cake\Controller\Controller.php, line 676
Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 189
Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 167
ControllerTestCase::_testAction() - CORE\Cake\TestSuite\ControllerTestCase.php, line 279
ControllerTestCase::__call() - CORE\Cake\TestSuite\ControllerTestCase.php, line 191
CategoryProblemsControllerTest::testAction() - APP\Test\Case\Controller\CategoryProblemsControllerTest.php, line 26
CategoryProblemsControllerTest::testIndex() - APP\Test\Case\Controller\CategoryProblemsControllerTest.php, line 26
ReflectionMethod::invokeArgs() - [internal], line ??
PHPUnit_Framework_TestCase::runTest() - C:\xampp_183\php\pear\PHPUnit\Framework\TestCase.php, line 976
PHPUnit_Framework_TestCase::runBare() - C:\xampp_183\php\pear\PHPUnit\Framework\TestCase.php, line 831
PHPUnit_Framework_TestResult::run() - C:\xampp_183\php\pear\PHPUnit\Framework\TestResult.php, line 648
PHPUnit_Framework_TestCase::run() - C:\xampp_183\php\pear\PHPUnit\Framework\TestCase.php, line 776
CakeTestCase::run() - CORE\Cake\TestSuite\CakeTestCase.php, line 82</pre></div></pre>
Fatal error: Call to a member function create() on a non-object in C:\xampp_183\htdocs\MDCW\Projects\services\app\Controller\CategoryProblemsController.php on line 53

Process finished with exit code 255

当我去C:\xampp_183\htdocs\MDCW\Projects\services\app\Controller\CategoryProblemsController.php on line 53时 他指出这行代码:$this->CategoryProblem->create(); 我该如何解决这个错误?对我的英语表示赞赏和抱歉

0 个答案:

没有答案