Laravel 5.6单元测试给出“失败断言为假”消息

时间:2018-11-28 11:46:51

标签: phpunit laravel-5.6 phpunit-testing

在我的laravel项目中,控制器文件设置为n个文件夹,如下所示:-

app/Http/Controllers/api/v2

MyController中的原始方法实际上是一个Web服务,它返回一个像这样的json数据-

{
    "login": {
          "status": 1,
          "message": "Login successful",
          "details": {
                "user_id": 2,
                "image": "http://localhost/demowebsite/public/images/general_images/no-user-image.png",
                "username": "xxxxxx@mail.com",
                "fname": "Saswat",
                "lname": "Routroy",
                "email": "xxxxxx@mail.com",
                "isd": "+91",
                "mobile": "xxxxxxxxxx",
                "created_date": "2018-07-12 08:09:06"
          },
          "error": [],
          "error_validation_key": [],
          "api_version": "1.0",
          "api_code": 1002
     }
}

这是我的单元测试代码

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class UserTest extends TestCase
{
    public function testUserLogin()
    {
        // #call($method, $uri, $parameters, $files, $server, $content, $changeHistory);

        $method     = 'POST';
        $uri        = 'api/v2/MyController@login';
        $parameters = array(
                        'email'         => 'xxxxxx@mail.com', 
                        'password'      => 'xxxxxx',
                        'auth_token'    => 'xxxxxxxxxxxxxxxxxxxxxxxxx',
                        'version'       => '2'
                    );
        $content    = array(
                        'xsrf-token' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                    );

        $response = $this->json($method, $uri, $parameters, [], [], $content);
        // you can check if response was ok 
        $this->assertTrue($response->isOk(), "Something went wrong");
    }
}

每当我运行单元测试时,都会出现以下错误:-

1) Tests\Unit\UserTest::testUserLogin
Something went wrong
Failed asserting that false is true.

/var/www/html/lightacandle/tests/Unit/UserTest.php:34

FAILURES!
Tests: 3, Assertions: 3, Failures: 1.

我在做什么错?有什么办法可以让我看到返回的json响应吗?

0 个答案:

没有答案