gpt4 book ai didi

php - 如何使用 Laravel Input::replace() 测试 POST 请求

转载 作者:可可西里 更新时间:2023-11-01 12:17:20 24 4
gpt4 key购买 nike

在单元测试期间,我在使用 Laravel 的 Input::replace() 方法模拟 POST 请求时遇到了一些问题。

根据 Jeffrey Way herehere 的说法,您可以这样做:

# app/tests/controllers/PostsControllerTest.php

public function testStore()
{
Input::replace($input = ['title' => 'My Title']);</p>

$this->mock
->shouldReceive('create')
->once()
->with($input);

$this->app->instance('Post', $this->mock);

$this->call('POST', 'posts');

$this->assertRedirectedToRoute('posts.index');
}

但是,我无法让它工作。 Input::all() 和所有 Input::get() 调用在 Input::replace() 之后仍然返回空数组或 null被使用。

这是我的测试函数:

public function test_invalid_login()
{
// Make login attempt with invalid credentials
Input::replace($input = [
'email' => 'bad@email.com',
'password' => 'badpassword',
'remember' => true
]);

$this->mock->shouldReceive('logAttempt')
->once()
->with($input)
->andReturn(false);

$this->action('POST', 'SessionsController@postLogin');

// Should redirect back to login form with old input
$this->assertHasOldInput();
$this->assertRedirectedToAction('SessionsController@getLogin');
}

尽管 $this->mock->shouldReceive() 不会被调用 $input - 它只会得到一个空数组。我在调试器中通过查看每个值的 Input::all()Input::get() 确认了这一点,它们都是空的。

TL/DR:如何在 Laravel 单元测试中发送带有 POST 数据的请求?

最佳答案

您应该使用 Request::replace(),而不是 Input::replace 来替换当前请求的输入数据。

关于php - 如何使用 Laravel Input::replace() 测试 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24050857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com