单元测试 Zend 控制器 - 如何测试视图中设置的内容
在 Zend 中,模型被添加到视图中:
//In a controller
public function indexAction() {
//Do some work and get a model
$this->view->model = $model;
}
我们可以轻松检查视图中是否存在“模型”(我为此使用 simpletest):
//In a unit test
public function testModelIsSetInView() {
//Call the controllers index action
$this->assertTrue(isset($this->controller->view->model));
}
但是,测试“值”也不起作用:
//In a unit test
public function testModelValue() {
//Call the controllers index action
//Both of these return null, though I'd like to access them!
$this->assertNull($this->controller->view->model);
$this->assertNull($this->controller->view->__get('model'));
}
How do I get (或至少测试)控制器已设置有效模型?
In Zend, models are added to the view:
//In a controller
public function indexAction() {
//Do some work and get a model
$this->view->model = $model;
}
We can easily check that "model" exists in the view (I'm using simpletest for this):
//In a unit test
public function testModelIsSetInView() {
//Call the controllers index action
$this->assertTrue(isset($this->controller->view->model));
}
However, testing the "value" doesn't work as well:
//In a unit test
public function testModelValue() {
//Call the controllers index action
//Both of these return null, though I'd like to access them!
$this->assertNull($this->controller->view->model);
$this->assertNull($this->controller->view->__get('model'));
}
How do I get (or at least test) that the controller has set a valid model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://www.contentwithstyle.co.uk/内容/带有 zend-framework 的单元测试控制器
http://www.contentwithstyle.co.uk/content/unit-testing-controllers-with-zend-framework
因此,解决方案(至少是目前计划的解决方案)是制作一个实现 Zend_View_Interface 的可测试视图。 这将包括一个“get”方法,该方法返回传递给“__set”的对象。 然后我们将连接控制器以在测试引导过程中使用此视图。
由于这可能不是最佳方法,我仍然很想听听其他有潜在解决方案的人的意见。
So, the solution (at least the planned one at this time) is make a testable view that implements Zend_View_Interface. This will include a "get" method that returns objects passed to "__set". Then we'll hook up the controller to use this view during the test bootstrapping process.
Since this may not be the optimal approach, I'd still love to hear from others who have potential solutions.