如何对 Laravel 控制器进行简单的测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22871503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do simple testing of a Laravel controller?
提问by eComEvo
In most web apps I've worked on, I can create a simple test script (test.php), load all dependencies (usually through an autoloader), setup the database connection, make calls to any class methods, then examine the results they return to make sure everything looks right.
在我使用过的大多数 Web 应用程序中,我可以创建一个简单的测试脚本 (test.php),加载所有依赖项(通常通过自动加载器),设置数据库连接,调用任何类方法,然后检查结果返回以确保一切正常。
Example:
例子:
$test = new Item;
var_dump($test->getItemStatus($itemid));
The above would show a nice output of the values it returns for that itemid.
上面将显示它为该 itemid 返回的值的一个很好的输出。
With Laravel it appears to be much more complex to just perform this simple test...or maybe I am overcomplicating it and there is a simple way to do this.
使用 Laravel,执行这个简单的测试似乎要复杂得多……或者我可能把它复杂化了,有一种简单的方法可以做到这一点。
Suppose I want to do the same thing in a Laravel 4 app. I want to test the output of method getItemStatus
in controller named ItemsController
that uses the model Items
.
假设我想在 Laravel 4 应用程序中做同样的事情。我想测试getItemStatus
名为ItemsController
使用模型的控制器中方法的输出Items
。
If I try the following, I get an Symfony \ Component \ Debug \ Exception \ FatalErrorException
error:
如果我尝试以下操作,则会收到Symfony \ Component \ Debug \ Exception \ FatalErrorException
错误消息:
$items = App::make('ItemsController')->getItemStatus($itemid);
If I define this route in routes.php:
如果我在 routes.php 中定义这条路线:
Route::get('items/get-item-status', array('as' => 'getItemStatus', 'uses' => 'ItemsController@getItemStatus'));
Then try the following, I get Symfony\Component\HttpKernel\Exception\NotFoundHttpException
in the request output:
然后尝试以下操作,我进入Symfony\Component\HttpKernel\Exception\NotFoundHttpException
请求输出:
$request = Request::create('items/get-item-status', 'GET', array());
$items = Route::dispatch($request)->getContent();
回答by Antonio Carlos Ribeiro
In my opinion in Laravel is much more simple and maybe that's why you might be overthinking it.
我认为在 Laravel 中要简单得多,也许这就是您可能想多了的原因。
Let's start with a very basic router. Here are some options to do simple debug using Laravel applications in a basic router/controller:
让我们从一个非常基本的路由器开始。以下是在基本路由器/控制器中使用 Laravel 应用程序进行简单调试的一些选项:
Route::get('test', function() {
$test = new Item;
dd($test);
Log::info($test); // will show in your log
var_dump($test);
});
Now follow:
现在跟随:
http://yourserver.com/test
And it should stop on
它应该停止
dd($test);
Vardumping and dying at that line. Comment it, try again, and so on.
Vardumping 和死在那条线上。评论它,再试一次,等等。
All of these will also work in a controller:
所有这些也将在控制器中工作:
Route::get('test/{var?}', 'TestController@index');
The very same way:
同样的方式:
class TestController extends Controller {
public function index($var = null)
{
$test = new Item;
dd($var);
dd($test);
Log::info($var);
Log::info($test);
var_dump($test);
var_dump($var);
}
}
Your route:
您的路线:
Route::get('items/get-item-status', array('as' => 'getItemStatus', 'uses' => 'ItemsController@getItemStatus'));
Should also work, if you follow:
也应该工作,如果你遵循:
http://yourserver.com/items/get-item-status
If it doesn't, try to follow
如果没有,请尝试关注
http://yourserver.com/public/index.php/items/get-item-status
Or just
要不就
http://yourserver.com/public/items/get-item-status
Because you might have a virtual host or .htaccess configuration problem.
因为您可能有虚拟主机或 .htaccess 配置问题。