dongshi1868 2015-04-08 06:53
浏览 47
已采纳

cakephp3 test component在非对象上调用on()上的成员函数

I'm trying to test my plugin component written for cakephp 3.

This is my component:

namespace CurrencyConverter\Controller\Component;

use Cake\Controller\Component;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\TableRegistry;

class CurrencyConverterComponent extends Component
{
    public $controller = null;

    public function setController($controller)
    {
        $this->controller = $controller;
    }

    public function startup($event)
    {
        $this->setController($event->subject());
    }


    public function convert($fromCurrency, $toCurrency, $amount, $saveIntoDb = 1, $hourDifference = 1, $dataSource = 'default') {

    }
}

and this is mt test:

namespace App\Test\TestCase\Controller\Component;

use CurrencyConverter\Controller\Component\CurrencyConverterComponent;
use Cake\Controller\Controller;
use Cake\Controller\ComponentRegistry;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\TestSuite\TestCase;

class CurrencyConverterComponentTest extends TestCase {
    public $fixtures = ['app.currencyconverter'];
    public $CurrencyConverter = null;
    public $controller = null;

    public function setUp() {
        parent::setUp();
        // Setup our component and fake test controller
        $request = new Request();
        $response = new Response();
        $this->controller = $this->getMock(
            'Cake\Controller\Controller',
            [],
            [$request, $response]
        );
        $registry = new ComponentRegistry($this->controller);
        $this->CurrencyConverter = new CurrencyConverterComponent($registry);
    }

    public function testAmountWithComma() {
        $fromCurrency   = 'EUR';
        $toCurrency     = 'GBP';
        $amount         = '20,00';
        $saveIntoDb     = 0;
        $hourDifference = 0;
        $dataSource     = 'test';

        $result = $this->CurrencyConverter->convert($fromCurrency, $toCurrency, $amount, $saveIntoDb, $hourDifference, $dataSource);

        $this->assertGreaterThan($result, $amount);
    }
}

When I run tests I get this error in the core!!

Fatal error:  Call to a member function on() on a non-object in /Users/alessandrominoccheri/Sites/cakephp3/vendor/cakephp/cakephp/src/Controller/Controller.php on line 289

How can I solve this?

Thanks

  • 写回答

1条回答 默认 最新

  • dsqbkh3630 2015-04-08 08:37
    关注

    I this particular case you are mocking too much. You are telling phpunit to mock all methods in the controller, including the eventManager() getter method, which makes the controller try to call on() on a null object.

    You need to only mock the methods you are interested in testing, that will alter the environment, or try to communicate with external services. Also, it seems like you are trying to test the Component instead of the Controller, the purpose of the test is not very clear.

    To me, it seems like your CurrencyConverter class should not be a component, but just a class in your project that you could use anywhere. There is no need to attach such a class to controllers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog