Laravel phpunit 测试获取带参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47474554/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:00:18  来源:igfitidea点击:

Laravel phpunit testing get with parameters

phplaravelphpunit

提问by MrAndre

I am writing some tests for my controllers but one of my tests doesn't work. It's supossed to search and get the results back to the page. But it's actually redirecting to the home page. Here is my code:

我正在为我的控制器编写一些测试,但我的一个测试不起作用。它可以搜索并将结果返回到页面。但它实际上是重定向到主页。这是我的代码:

use DatabaseMigrations;
protected $user;
public function setUp()
{
    parent::setUp();

    $this->seed();

    $this->user = factory(User::class)->create(['role_id' => 3]);
}

/** @test */
public function test_manage_search_user()
{
    $response = $this->followingRedirects()->actingAs($this->user)->get('/manage/users/search', [
        'choices' => 'username',
        'search' => $this->user->username,
    ]);

    $response->assertViewIs('manage.users');
    $response->assertSuccessful();
    $response->assertSee($this->user->email);
}

The URL you should get to make it work look like this:

您应该获得的 URL 如下所示:

http://localhost/manage/users/search?choices=username&search=Test

I checked again and it looks like it's not given in the parameters with the get request. How can I fix this?

我再次检查,看起来它没有在get request参数中给出。我怎样才能解决这个问题?

回答by MathieuAuclair

I had the same issue trying to test GETRequests, you actually can't pass parameter with the $this->get('uri', [header])but you can by using $this->call, if you check in MakesHttpRequests.phpyou can see that this->get()is actually using call method.

我在尝试测试GET请求时遇到了同样的问题,您实际上无法使用 传递参数,$this->get('uri', [header])但是您可以使用$this->call,如果您检查MakesHttpRequests.php,您可以看到它this->get()实际上使用的是调用方法。

By adding an array to get method, you are changing the request headers, this is why you are not getting your parameters.

通过向 get 方法添加数组,您正在更改请求标头,这就是您没有获取参数的原因。

public function get($uri, array $headers = [])
{
    $server = $this->transformHeadersToServerVars($headers);

    return $this->call('GET', $uri, [], [], [], $server);
}

public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
{
    $kernel = $this->app->make(HttpKernel::class);

    $files = array_merge($files, $this->extractFilesFromDataArray($parameters));

    $symfonyRequest = SymfonyRequest::create(
        $this->prepareUrlForRequest($uri), $method, $parameters,
        $cookies, $files, array_replace($this->serverVariables, $server), $content
    );

    $response = $kernel->handle(
        $request = Request::createFromBase($symfonyRequest)
    );

    if ($this->followRedirects) {
        $response = $this->followRedirects($response);
    }

    $kernel->terminate($request, $response);

    return $this->createTestResponse($response);
}

So if you want to test a GETRequest you will have to do this:

所以如果你想测试一个GET请求,你必须这样做:

$request = $this->call('GET', '/myController', ["test"=>"test"]);

In your controller you should be able to get theses parameters like so:

在您的控制器中,您应该能够像这样获得这些参数:

public function myController(Request $request)
{
    $requestContent = $request->all();
    $parameter = $requestContent['test'];
}

回答by Hyder B.

I'm using Laravel 5.X (more precisely 5.6), you can pass custom parameters using:

我正在使用 Laravel 5.X(更准确地说是 5.6),您可以使用以下方法传递自定义参数:

 $response = $this->json('GET',  '/url/endpoint',['params'=>'value']);

回答by shxfee

You can use the route helper to build a url with query string. in your case i would do something like this. Assuming the route name is manage.users.search

您可以使用路由助手构建带有查询字符串的 url。在你的情况下,我会做这样的事情。假设路由名称是 manage.users.search

$route = route('manage.users.search', [
    'choices'=> 'username',
    'search' => $this->user->username,
]);

$response = $this->followingRedirects()
    ->actingAs($this->user)
    ->get($route);

回答by Ben

You could use the request helper to merge in http get parameters as such:

您可以使用请求助手合并 http 获取参数,如下所示:

    /** @var \Illuminate\Http\Request $request */
    $request = request();
    $request->merge([
        'choices' => 'username',
        'search' => 'Test'
    ]);

回答by Raza Mehdi

I would do it like this:

我会这样做:

$this->actingAs($this->user);

$response = $this->get('/manage/users/search', [
    'choices' => 'username',
    'search' => $this->user->username,
]);

$response->assertViewIs('manage.users');
$response->assertSuccessful();
$response->assertSee($this->user->email);