【问题标题】:How to post data in from back-end testcase to controller如何将数据从后端测试用例发布到控制器
【发布时间】:2019-10-16 11:22:04
【问题描述】:

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

项目点添加功能

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"
}.

但我希望看到测试用例成功,并且在检查数据库时没有记录添加到数据库中

【问题讨论】:

    标签: laravel post laravel-5 phpunit


    【解决方案1】:

    您是否尝试过仍将 locationarea 放入您的可填充项中?

    【讨论】:

    • 是的,我试过了。但不幸的是它并没有解决我的问题。
    猜你喜欢
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2013-05-12
    相关资源
    最近更新 更多