Php 如何在Laravel5上使用单元测试检查路由
我尝试过使用单元测试,但它不起作用。不知怎的,收到的状态码是404。然后,终端声明原因是异常“Symfony\Component\HttpKernel\exception\NotFoundHttpException” Routes.phpPhp 如何在Laravel5上使用单元测试检查路由,php,unit-testing,laravel,laravel-5,Php,Unit Testing,Laravel,Laravel 5,我尝试过使用单元测试,但它不起作用。不知怎的,收到的状态码是404。然后,终端声明原因是异常“Symfony\Component\HttpKernel\exception\NotFoundHttpException” Routes.php Route::get('/', function() { return view('toppage')->with('contents', ""); }); ExampleTest.php use Illuminate\Foundation\T
Route::get('/', function() {
return view('toppage')->with('contents', "");
});
ExampleTest.php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$this->visit('/')
->see('Where lane');
}
}
nignx.conf(从文件中摘录一部分)
如果您想查看是否可以访问路线。最好验证该链接上的文本 试试这个:
class WelcomeTest extends TestCase {
/**
* Test if the public index page returns the correct view
* GET /
*/
public function testWelcomePage()
{
$response = $this->call('GET', '/');
// check if we're getting the default welcome page.
//you can replace this with something on your default route's <title> Html tag
$this->assertRegexp('/<title>Welcome<\/title>/', $response->getContent());
}
}
class WelcomeTest扩展了TestCase{
/**
*测试公共索引页是否返回正确的视图
*得到/
*/
公共函数testWelcomePage()
{
$response=$this->call('GET','/');
//检查我们是否获得默认的欢迎页面。
//您可以将其替换为默认路由的Html标记上的内容
$this->assertRegexp('/Welcome/',$response->getContent());
}
}
如果失败,请先尝试在浏览器中获取站点。清除路线、缓存并仔细检查页面上是否存在文本“Where lane”。谢谢您的建议。基于你的想法,我创建了一个测试并检查它是否有效!!然后,我检查了代码,$this->visit('/')没有像我预期的那样工作。由于NotFoundHttpException,结果失败。虽然我不明白详细的原因,但我会用你的想法创建测试用例WelcomeController@index“);”然后运行“php artisan路由:缓存”验证路由是否指向正确的位置“php artisan路由:列表”我认为这应该使$this->visit('/')也能工作。让我知道进展如何。谢谢你的额外建议。至少我没有在终端上输入这些命令。所以,如果问题解决了,我会发布答案。在验证路由后,我尝试了“$this->visit('/')”,但它返回失败,返回代码为404。但是,代码“$this->get('/')”起了作用。以防万一,我向你报告了。
class WelcomeTest extends TestCase {
/**
* Test if the public index page returns the correct view
* GET /
*/
public function testWelcomePage()
{
$response = $this->call('GET', '/');
// check if we're getting the default welcome page.
//you can replace this with something on your default route's <title> Html tag
$this->assertRegexp('/<title>Welcome<\/title>/', $response->getContent());
}
}