codeception laravel seeResponseContainsJson不匹配

时间:2014-07-23 16:57:34

标签: laravel-4 phpunit codeception

我已经通过服务器回复了这个回复:

  

)]}',{"成功":0,"错误":{"错误":["无效的用户名或   。密码"]}}

我无法继续工作seeResponseContainsJson 我试过了:

public function invalidUserShouldBe200AndJsonTest(AcceptanceTester $I)
    {
        $I->wantTo('To see a 200 status code and a json response');
        $I->sendAjaxPostRequest('/api/v1/signin',array('email'=>'user@users.com','password'=>'user'));
        $I->seeResponseCodeIs('200');
        $I->seeResponseIsJson();
        $I->seeResponseContainsJson(array('errors' => array('error' => array('Invalid username or password.'))));
    }

但我已经

  

我看到回复包含json {"错误":{"错误":["无效的用户名或   。密码"]}}

如果响应是

,它也是一样的
  

{"成功":0,"错误":{"错误":["无效的用户名或密码。"]}}

如果我尝试

,那么mmm应该是错误的
$I->seeResponseIsJson();
dd($I->grabResponse());

seeResponseIsJson给了我一个绿色的酒吧但抓住了响应给我"" :(

我的测试

use \AcceptanceTester;

class SessionControllerCest
{

    public function _before(\AcceptanceTester $I)
    {
        $I->wantTo('Grab the XSRF-TOKEN cookie value');
        $I->amOnPage('/');
        $text = $I->grabTextFrom('body > div > script');
        $chunks1 = explode("\n", trim($text));
        $chunks2 = explode("=", trim($chunks1[1]));
        $cookieValue = rtrim($chunks2[2], ';"');
        $I->setCookie('XSRF-TOKEN',$cookieValue);
        $I->setHeader('X-XSRF-TOKEN',$cookieValue);
    }

    public function _after()
    {
    }

    // tests
    public function invalidUserShouldBe200AndJsonTest(AcceptanceTester $I)
    {
        $I->wantTo('To see a 200 status code and a json response');
        $I->sendAjaxPostRequest('/api/v1/signin',array('email'=>'user@user.com','password'=>'sentryuser'));
        $I->seeCookie('XSRF-TOKEN');
        $I->seeResponseCodeIs('200');
        $I->seeResponseIsJson();// until here green bar
       // dd($I->grabResponse()); // empty string
        //$I->seeResponseContainsJson(array('success' => 0,'errors' => array('error' => array('Invalid username or password.'))));

        $I->seeResponseContains(""); // red bar 
    }
}

由于跨站点请求伪造(XSRF)保护,我在_之前设置了这样的内容 它就像

Route::filter('xhr', function()
{
    if(!Request::ajax()){
        return Response::make('Not Found', 404);
    } 

});

Route::filter('xsrf', function()
{
    if((!isset($_COOKIE['XSRF-TOKEN']) || is_null(Request::header('X-XSRF-TOKEN'))) || ($_COOKIE['XSRF-TOKEN'] !== Request::header('X-XSRF-TOKEN'))){
        return Response::make('Not Found', 404);
    }
});

路线

Route::group(array('prefix' => 'api/v1', 'before' => 'xhr|xsrf'), function() {
    /* Session */
    Route::post('signin', array('as' => 'session.store', 'uses' => 'App\Controllers\SessionController@store'));
});

DEBUG

如果我评论_before块,那就有问题了 试图发送一个cUrl请求,如:

curl -H 'X-Requested-With: XMLHttpRequest' -d "email=user@user.com" \
-d "password=mysentryuser" \
http://lama.io/api/v1/signin

我得到了

  

{"成功":0,"错误":{"错误":["无效的用户名或密码。"]} }

但是当我运行这个

// tests
    public function invalidUserShouldBe200AndJsonTest(AcceptanceTester $I)
    {
        $I->wantTo('To see a 200 status code and a json response');
        $I->sendAjaxPostRequest('/api/v1/signin',array('email'=>'user@user.com','password'=>'mysentryuser'));
       // $I->seeCookie('XSRF-TOKEN');
        $I->seeResponseCodeIs('200');
       //$I->seeResponseIsJson();// until here green bar
       //dd($I->grabResponse()); // empty string
       $I->seeResponseContainsJson(array('success' => 0,'errors' => array('error' => array('Invalid username or password.'))));

        //$I->seeResponseContains(""); // red bar 
    }

我得到了

  

我看到回复包含json {"成功":0,"错误":{"错误":["无效   用户名或密码。"]}}

所以可能有些不对劲但是什么!

1 个答案:

答案 0 :(得分:0)

您可以按如下方式重写测试的最后一行:

$I->seeResponseContainsJson(array('success' => 0, 'errors' => array('error' => array('Invalid username or password.'))));

因为那是你的回复包含的JSON。