简体   繁体   English

Laravel 5.2 PHPUnit JSON Api请求正文未设置

[英]Laravel 5.2 PHPUnit JSON Api request body not being set

I am testing POSTing data to an API endpoint we've created using Laravel 5.2, but none of the parameters seem to be reaching the application in the test. 我正在测试POST数据到我们使用Laravel 5.2创建的API端点,但是没有一个参数似乎在测试中到达应用程序。 The endpoint expects json and responds with json and uses a FormRequestValidator which has required rules for active and make parameters. 端点需要json并使用json进行响应,并使用FormRequestValidator,它需要active和make参数的规则。 The test fails with status code 422 and the examining the response body it states the active and make parameters are required even though they are being passed in the call so therefore for some reason when the request reaches the the Form Request Validator, the input is not there. 测试失败,状态代码为422,并检查响应主体,它指出active和make参数是必需的,即使它们是在调用中传递的,因此由于某种原因,当请求到达Form Request Validator时,输入不是那里。

However, when I invoke the endpoint with json body including make and active from Postman or the UI app we've built it works fine, it is only failing in the PHPUnit tests therefore it must be something with PHPUnit or the test setup being incorrect. 但是,当我使用json主体调用端点,包括来自Postman的make和active或我们构建的UI应用程序时,它工作正常,它只是在PHPUnit测试中失败,因此它必须是PHPUnit或测试设置不正确的东西。 Here is the test: 这是测试:

public function testItStoresCars()
{
    // Arrange
    $user = User::first();

    //Act
    $this->json(Request::METHOD_POST, '/v1/cars', [
        'active' => true,
        'make' => 'Audi'
    ], 
    ['Authorization' => 'Bearer '.\JWT::fromUser($user)]));

    // Assert
    $this->assertResponseOk();
}

I know the Authorisation header is set correctly from other tests passing. 我知道从其他测试传递中正确设置了Authorization标头。

I've tried disabling middleware, using the $this->post helper method and manually setting the headers as well as using the $this->call method with setting the Headers and encoding the data using json_encode but I always get the same 422 response. 我已经尝试过禁用中间件,使用$this->post helper方法并手动设置标头以及使用$this->call方法设置Headers并使用json_encode对数据进行编码,但我总是得到相同的422响应。 I'm wondering has anyone encountered this issue or can see an error? 我想知道是否有人遇到此问题或者可以看到错误?

Controller Code 控制器代码

public function store(CreateCarRequest $request)
{
    $car = $this->carRepo->save($request->all());

    return response()->json(car);
}

FormRequest FormRequest

class CreateCarRequest extends Request
{
public function rules()
{
    return [
        'active' => 'required|boolean',
        'make' => 'required',
    ];
}
}

422 is the error response for validation errors.. which means either your data is not posted or it doesn't pass server validation, try 422是验证错误的错误响应..这意味着您的数据未发布或未通过服务器验证,请尝试

$this->json(Request::METHOD_POST, '/v1/cars', [
        'active' => true,
        'make' => 'Audi'
    ], 
    ['Authorization' => 'Bearer '.\JWT::fromUser($user)]))->dump();

dump() should show you the errors dump()应该显示错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM