【问题标题】:Check response of validation in codeception检查代码接收中的验证响应
【发布时间】:2017-06-07 20:11:56
【问题描述】:

我使用 laravel 用这种方法创建了一个 php:

  public function register(Request $request) {
    try {
        $validator = \Validator::make($request->input(), [
                    'email' => 'required|email|unique:users_x_communities',
                    'first_name' => array('required', 'regex:/^[\pL\s]+$/u'),
                    'last_name' => array('required', 'regex:/^[\pL\s]+$/u'),
                    'community_id' => 'required|exists:communities,id',
                        ], [
                    'email.required' => 'El correo electrónico es requerido.',
                    'email.email' => 'El correo electrónico es inválido.',
                    'email.unique' => 'El correo electrónico ya esta en uso.',
                    'first_name.required' => 'Los nombres son requeridos.',
                    'first_name.regex' => 'Los nombres deben ser un texto.',
                    'last_name.required' => 'Los apellidos son requeridos.',
                    'last_name.regex' => 'Los apellidos deben ser un texto.',
                    'community_id.required' => 'La comunidad es requerida.',
                    'community_id.exists' => 'La comunidad no existe.'
                        ]
        );
        if ($validator->fails()) {
            return $this->errorBadRequest($validator->messages());
        }
        $email = strtolower($request->get('email'));
        $firstName = $request->get('first_name');
        $lastName = $request->get('last_name');
        $communityId = $request->get('community_id');
        $status = $this->auth->register($firstName, $lastName, $email, $communityId);
        $message = "OK!";

        return $this->response->array(compact('status', 'message'));
    } catch (Exception $e) {
        Log::error('AuthController@register: ' . $e->getMessage());
        return $this->validException($e);
    }
}

我有兴趣在 codeception 中进行一些测试以验证我的功能,在这种情况下需要 4 个字段,email、first_name、last_name 和 community_id。

我在 codeception (ResgisterCest.php) 中管理了这个:

    public function validateifmailisrequired(ApiTester $I) //200
    {

            //Method created to validate if the mail is required

            $I->wantTo('Validate that the mail field is required');
            $I->sendPOST($this->url, [
                'email' => '',
            ]);

    }

但我有点新,我不太确定如何检查电子邮件,例如,需要的响应。我是新手,对代码接收有点困惑……我怎样才能做到这一点?让 codeception 告诉我,如果我用空邮件 sendPost 返回我一个必填字段的回复或一些让我测试我的电子邮件和其他人是否需要的回复....

【问题讨论】:

    标签: php codeception


    【解决方案1】:

    使用seeResponseContainsJson 方法。

    $I->sendPOST($this->url, ['email' => '']);
    $I->seeResponseContainsJson(['error' => 'El correo electrónico es requerido.']);
    

    使期望数组的结构与响应的结构相匹配 (但不要指定所有键,只指定对本次测试重要的键)。

    【讨论】:

    • 我在控制台上得到一个巨大的红色屏幕和一个 [InvalidArgumentException] Invalid json: 你能帮我吗,我很难处理这个问题,没有把它填满,就像卡住了 4 天这。谢谢
    • 请在您的问题中添加完整的异常消息,您的回复存在问题。
    • 好吧,我得到了 [InvalidArgumentException] Invalid json: \ \ \... 后面是看起来像整个 html 页面的内容(从那以后我没有发表意见我只是在做 api 测试),最后它说语法错误......它太长了,不能在这里粘贴......
    • 你必须弄清楚为什么你的 url 返回 HTML。也许您需要在发出请求之前设置一些请求标头,例如$I->haveHttpHeader('Accept', 'application/json');
    • 我在我的 RegisterCest.php 公共函数上有这个 _before(ApiTester $I) { $I->haveHttpHeader('Content-Type', 'application/x-www-form-urlencoded'); } 这不应该照顾那个吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    相关资源
    最近更新 更多