所以我给出了我的 Controller 和 web.php 文件的示例。
controller
{
public function add()
{
//Method called via web.php
}
public function multiply()
{
//how to access this while testing
}
}
网络.php
route::get('/add','controller@add');
现在我想进行单元测试
case1: add $response = $this->get(route('add'));
现在在访问/add
时会发送一个get请求并且 Controller 方法 add
将被访问。然后我可以使用各种断言来检查它的功能。
case2: multiply
这里没有指定任何路由,因此无法访问 Controller 方法。
在这种情况下如何测试我的乘法
更新的答案
所以从所有的答案和评论中,我了解到在 Laravel 中
- 单元测试 - 创建一个 Controller 实例,然后调用您要测试的方法。
- 功能测试 - 在这种测试模式下,我们将调用路由并测试是否已命中正确的方法并且它是否按预期工作。
谢谢!!
最佳答案
您可以像下面这样在测试文件中创建 Controller 对象。
public function testBasicTest()
{
$object = (new UsersController());
$response = $object->methodName();
$this->assertTrue($response);
}
关于php - 测试未链接到任何路由的 Controller 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55241099/