如何开始测试 Zend 框架模型?
如何开始在 Zend Framework 1.8+ 应用程序中测试我的模型?
假设我已设置应用程序以开始测试。我已经测试过控制器,所以我知道它可以工作。我的所有控制器都扩展了我的 ControllerTestCase.php
文件:
<?php
require_once 'Zend/Application.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
public $application;
public function setUp()
{
$this->application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$this->bootstrap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
{
$this->application->bootstrap();
}
public function tearDown()
{
Zend_Controller_Front::getInstance()->resetInstance();
$this->resetRequest();
$this->resetResponse();
$this->request->setPost(array());
$this->request->setQuery(array());
parent::tearDown();
}
}
但现在我想开始测试我的模型。看起来我的 ModelTestCase.php
不会扩展 Zend_Test_PHPUnit_ControllerTestCase
,而是扩展 Zend_Test_PHPUnit_ModelTestCase
,但据我所知,不存在这样的类。如何开始测试我的 Zend Framework 模型?
How do I begin testing my models in a Zend Framework 1.8+ application?
Let's say I have my application set up to start testing. I have already tested a controller, so I know it works. I have all my controllers extending my ControllerTestCase.php
file:
<?php
require_once 'Zend/Application.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
public $application;
public function setUp()
{
$this->application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$this->bootstrap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
{
$this->application->bootstrap();
}
public function tearDown()
{
Zend_Controller_Front::getInstance()->resetInstance();
$this->resetRequest();
$this->resetResponse();
$this->request->setPost(array());
$this->request->setQuery(array());
parent::tearDown();
}
}
But now I want to start testing my models. It seems like my ModelTestCase.php
would not extend Zend_Test_PHPUnit_ControllerTestCase
but rather a Zend_Test_PHPUnit_ModelTestCase
, but no such class exists that I know of. How can I start testing my Zend Framework models?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为您提供了一个基本的
ControllerTestCase
,因为设置和拆除测试控制器的环境需要复杂的步骤。输入是模拟 HTTP 请求,输出是呈现的 HTML,您需要抓取该 HTML 来查找预期内容。模型更像是一个普通的旧 PHP 对象。需要设置的环境较少。接口只是对对象的方法调用。
因此,我将启动一个
TestCase
类来扩展 PHPUnit 的普通TestCase
,并首先添加至少一个test
方法(作为 空function)用于模型类中的每个方法。最终,您的模型类中的每个方法都会有许多测试方法,但创建空测试方法是防止忘记某些模型方法的好方法。请注意,模型不是表 --模型通常使用一个或多个Table对象。通过遵循此模式,您有机会创建 表的模拟对象,这样您就可以运行测试套件,而无需实时连接到数据库。
下面是设置模拟 Table 对象的示例,该对象经过硬编码以返回合成数据集而不是数据库中的数据集。
There is a base
ControllerTestCase
provided for you because there are complex steps needed to setup and tear down the environment for testing a controller. The input is a mock HTTP request, and the output is rendered HTML that you need to scrape to find expected content.A Model is more like a plain old PHP object. There's less environment to set up. The interface is simply method calls to the object.
So I would start a
TestCase
class that extends PHPUnit's plainTestCase
, and start by adding at least onetest
method (as an empty function) for each method in your Model class. You will eventually have many test methods for each method in your Model class, but creating the empty test methods is a good way to keep from forgetting some of your Model methods.Note that a Model is not a Table -- a Model typically uses one or more Table objects. By following this pattern, you have the opportunity to create mock objects for Tables so you can run the test suite without requiring a live connection to a database.
Here's an example of setting up a mock Table object, which is hardcoded to return a synthetic data set instead of a data set from a database.