laravel 单元测试怎么写?举例说明

laravel 单元测试例子

这里拿 laravel JWT (前提:你的 laravel 项目已经集成 JWT,没有的可以去借鉴教程:http://www.cnblogs.com/love-snow/articles/7567453.html):

laravel 单元测试怎么写?举例说明_第1张图片

1、生成一个测试文件 tests/Feature/UserTest.php

php artisan make:test UserTest

laravel 单元测试怎么写?举例说明_第2张图片

2、tests/Feature/UserTest.php 写测试脚本:

php

namespace Tests\Feature;

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

class UserTest extends TestCase
{
     use DatabaseTransactions;

    /** @test */
    public function it_can_register_a_user()
    {
        
        $response = $this->json('POST', '/api/register', [
                'name'     => 'test', 
                'email'    => '[email protected]',
                'password' => '12345'
            ]);
        
        $response->assertStatus(200)->assertJsonFragment(['result' => 'register success']);
    }

    /** @test */
    public function it_can_get_a_token()
    {
        $this->it_can_register_a_user();
        $response = $this->json('POST', '/api/login', [
                'name'     => 'test',
                'password' => '12345'
            ]);
        
        $response->assertStatus(200);

        return json_decode($response->getContent())->token;
    } 

    /**  @test */
    public function it_can_get_user_details()
    {
        $token    = $this->it_can_get_a_token();
        $response = $this->json('GET', "/api/get_user_details?token={$token}");

        $response->assertStatus(200)->assertJsonFragment([
                'name'     => 'test', 
                'email'    => '[email protected]'
            ]);
    }
}

3、运行测试:

phpunit --filter UserTest

laravel 单元测试怎么写?举例说明_第3张图片

测试成功!

单元测试,怎么添加测试数据?

laravel 单元测试怎么写?举例说明_第4张图片

1、首先生成一个 factory 文件:

php artisan make:factroy TerminalFactory

laravel 单元测试怎么写?举例说明_第5张图片

2、修改 database/factories/TerminalFactory.php 文件:

php

use Faker\Generator as Faker;

$factory->define(App\Models\Terminal::class, function (Faker $faker) {
    $faker = \Faker\Factory::create('zh_CN');//这里使用中文填充,默认不写是英文
    return [
        'user_id'        => 1,
        'name'           => $faker->unique()->name,
        'screen_id'      => 1,
        'number'         => $faker->unique()->swiftBicNumber,
        'mac'            => $faker->unique()->macAddress,
        'type'           => 'test_MS801',
        'direction'      => '0',
        'version_number' => 'Test-000',
        'determ_number'  => $faker->unique()->randomDigit,
    ];
});

提示:填充数据的数据类型及格式都有哪些?参考我的博文地址:http://www.cnblogs.com/love-snow/articles/7655450.html

3、编写你的测试文件 tests/Feature/TerminalTest

php

namespace Tests\Feature;

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

class TerminalTest extends TestCase
{
    use DatabaseTransactions;

    /** @test */
    public function it_can_gets_a_list_of_terminals_by_screen_id()
    {
        factory(\App\Models\Terminal::class,3)->create();
        $response = $this->get("/web/terminals?screen_id=1");
        
        $response->assertStatus(200)->assertJsonFragment(['screen_id' => 1]);
    }
}

TerminalController 是这样的:

php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;use App\Models\Terminal;use Illuminate\ Http\Request;
class TerminalController extends Controller
{
    /**
     * 显示当前用户下的终端.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $user_id = $this->user_id;
        $terminal = Terminal::where(['user_id' => $user_id, 'screen_id' => $request->screen_id])->get();
        
        return response()->json($terminal, 200);
    }

运行测试文件即可!

 

转载于:https://www.cnblogs.com/love-snow/articles/7656716.html

你可能感兴趣的:(laravel 单元测试怎么写?举例说明)