2

Hello I write test and want to get data from DB in it, so I do next

Company.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Company extends Model
{
    protected $table = 'companies';
}

MyTest.php

<?php
use App\Models\Company;

class MyTest extends TestCase {

    public function testFileUploadAction()
    {
        $company = Company::find(1);
    }


}

but have error

Fatal error: Call to a member function connection() on a non-object in /myproject/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 3146

So how I can get data From DB in test?

Update: all my tests work fine issue only in get data from DB in test.

Model work fine.

**Update I found solution error was because application wasn't booted so I add

$app = require __DIR__.'/../bootstrap/app.php';

$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

and all work fine

4
  • Please add routes.php and your App\Models\Company object Commented May 8, 2015 at 7:51
  • routes.php? what it will show you? Commented May 8, 2015 at 7:56
  • The line that refers to your MyTestController. I actually ope that TestCase class is extending BaseController tat comes with laravel is it? Commented May 8, 2015 at 7:57
  • I do like this laravel.com/docs/5.0/testing#defining-and-running-tests Commented May 8, 2015 at 8:14

1 Answer 1

2

I found solution error was because application wasn't booted so I add

$app = require __DIR__.'/../bootstrap/app.php';

$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

and all work fine

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.