我正在使用CakePHP-ReST-DataSource-Plugin数据源在我的模型中打击RESTful服务。这意味着模型不会有数据库连接。在模型TestCase中为CakePHP模拟REST数据源
我已经成功地访问了服务,现在想写模型的单元测试。这被证明是一项艰巨的任务,因为我无法成功模拟数据源,因此我没有打到实际的远程服务,而是返回测试结果。
<?php
App::uses('KnowledgePoint', 'Model');
class KnowledgePointTest extends CakeTestCase{
public $fixtures = array('app.knowledgepoint');
public $useDbConfig = 'RestTest';
private $KnowledgePoint;
public function setUp() {
parent::setUp();
$this->KnowledgePoint = ClassRegistry::init('KnowledgePoint');
/**
* This is the confusing part. How would I mock the datasource
so that I can mock the request method which returns the data
from the api?
*/
$this->KnowledgePoint->DataSource = $this->getMockForModel(
'RestSource',array('request'));
}
public function tearDown() {
parent::tearDown();
}
}
我想能够嘲笑数据源和存根请求方法返回通常会被从远程服务返回的数据。
亲切的问候,
罗兰
检查核心是如何做到的:** https://github.com/cakephp/cakephp/blob/2.5.6/lib/Cake/Test/Case/Model/Datasource/DataSourceTest.php#L110** – ndm 2014-11-25 01:29:50
我试着嘲笑它的核心做法,但我不断收到TestSource模拟找不到的错误。 – Awemo 2014-11-25 13:23:23
核心测试也失败了吗? – ndm 2014-11-25 13:26:41