如何将数据从后端测试用例发布到控制器

时间:2019-05-30 19:20:06

标签: laravel post laravel-5 phpunit

我是php的新手,我目前正在尝试为我为在数据库的“ project_point”表中添加记录而编写的add函数创建一个测试用例。在这个测试用例中,我要向该添加功能发布一些测试数据,并检查数据是否设置正确。

项目点添加功能

public function addProjectPoint (Request $request) {
    $point = new ProjectPoint();

    $location = new Point($request->markerLat, $request->markerLong);

    $point->project_id = $request->project_id;
    $point->location = $location;
    $point->area = $request->area;
    $point->name = $request->name;
    $point->information = $request->information;
    $point->category = $request->category;

    $point->save();
}

我的测试用例

public function testCreateProjectPoint()
    {
        $this->post('admin/projectpoint/create', [
            'project_id' => 1,
            'markerLat' => 5.287020206451416,
            'markerLong' => 51.68828138589033,
            'area' => null,
            'name' => 'TestCaseProjectPoint',
            'information' => 'This is a automated test ProjectPoint, please delete this point!',
            'category' => 'bezienswaardigheid'
        ]);

        $this->assertDatabaseHas('interest_point', [
            'project_id' => 1,
            'location' => new Point(5.287020206451416, 51.68828138589033),
            'area' => null,
            'name' => 'TestCaseProjectPoint',
            'information' => 'This is a automated test ProjectPoint, please delete this point!',
            'category' => 'bezienswaardigheid'
        ]);

        /*
        $test = factory(ProjectPoint::class)->create();

        $this->post('admin/projectpoint/create', $test);

        $this->assertDatabaseHas('project_point', $test);
        */
    }

ProjectPoint模型

class ProjectPoint extends Model
{
    use SpatialTrait;

    protected $table = 'interest_point';

    protected $fillable = ['project_id', 'name', 'information', 'category' ];

    protected $spatialFields = [
        'location',
        'area'
    ];

    public $timestamps = false;

    public function project()
    {
        return $this->belongsTo('App\Models\Project', 'project_id');
    }
}

测试的输出是:

Failed asserting that a row in the table [interest_point] matches the attributes {
    "project_id": 1,
    "location": {
        "type": "Point",
        "coordinates": [
            51.68828138589033,
            5.287020206451416
        ]
    },
    "area": null,
    "name": "TestCaseProjectPoint",
    "information": "This is a automated test ProjectPoint, please delete this point!",
    "category": "bezienswaardigheid"
}.

但是我希望看到测试用例成功,并且在检查数据库时未将任何记录添加到数据库中

1 个答案:

答案 0 :(得分:0)

您是否尝试过将locationarea放入可填充对象?

相关问题