谁能提供一个使用测试驱动开发符号在Symfony2中进行开发的标准示例?或者分享TDD Symfony2开发的有趣材料的链接(官方文档除外:)?
附注:有人在为MVC模式中的控制器编写单元测试吗?
发布于 2011-06-28 02:08:45
我只是为基于Symfony2的微框架silex做了这件事。据我所知,它们非常相似。我推荐它作为Symfony2-world的入门读物。
我还使用TDD创建了这个应用程序,所以我所做的是:
我写了我的第一个测试来验证route/action
一个示例测试用例(在tests/ExampleTestCase.php中)如下所示:
<?php
use Silex\WebTestCase;
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
class ExampleTestCase extends WebTestCase
{
/**
* Necessary to make our application testable.
*
* @return Silex\Application
*/
public function createApplication()
{
return require __DIR__ . '/../bootstrap.php';
}
/**
* Override NativeSessionStorage
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->app['session.storage'] = $this->app->share(function () {
return new ArraySessionStorage();
});
}
/**
* Test / (home)
*
* @return void
*/
public function testHome()
{
$client = $this->createClient();
$crawler = $client->request('GET', '/');
$this->assertTrue($client->getResponse()->isOk());
}
}我的bootstrap.php
<?php
require_once __DIR__ . '/vendor/silex.phar';
$app = new Silex\Application();
// load session extensions
$app->register(new Silex\Extension\SessionExtension());
$app->get('/home', function() use ($app) {
return "Hello World";
});
return $app;我的web/index.php
<?php
$app = require './../bootstrap.php';
$app->run();https://stackoverflow.com/questions/5931912
复制相似问题