jeudi 30 mai 2019

How to post data in my testcase to controller

I am new to php and I am currently trying to make a testcase for an add function I wrote for adding records in the table "project_point" of my database. In this testcase I want to post some test data to that add function and check if the data is set correctly.

Project Point Add function

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();
}

My test case

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 model

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');
    }
}

The output of the test is:

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

But I expect to see the test case succeed and when checking the database no records have been added to the database



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2Kb4Wkt
via IFTTT

Aucun commentaire:

Enregistrer un commentaire