title: Laravel 5 Unit Test - Call to a member function connection() on null
body: <p>I tried creating a unit test for the relationships between my <code>User</code> and <code>Shop</code> models, however when I run <code>vendor\\bin\\phpunit</code> this error(s) are thrown, I have no idea about this since I'm a newbie in unit testing. I tried to run my code on my controller to see if the relationship actually works, and fortunately it is working as expected, but not when run in phpunit. What have I done wrong for this phpunit not to work with Models?</p>
  <blockquote>
  <p><strong><code>Fatal error:</code></strong> <em><code>Uncaught Error: Call to a member function connection() on null in E:\projects\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1013</code></em></p>
  <p><strong><code>Stack trace:</code></strong>
  <em><code>E:\projects\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(979): Illuminate\Database\Eloquent\Model::resolveConnection(NULL)</code></em></p>
  </blockquote>
  <p>This is my UserTest.php</p>
  <pre><code>&lt;?php
  
  namespace Tests\Unit;
  
  use Tests\TestCase;
  use Illuminate\Foundation\Testing\DatabaseMigrations;
  use Illuminate\Foundation\Testing\DatabaseTransactions;
  
  use App\User;
  use App\Shop;
  
  class UserTest extends TestCase
  {
  
      protected $user, $shop;
  
      function __construct()
      {
          $this-&gt;setUp();
      }
      
      function setUp()
      {
          $user = new User([
              'id' =&gt; 1,
              'first_name' =&gt; 'John',
              'last_name' =&gt; 'Doe',
              'email' =&gt; 'JohnDoe@example.com',
              'password' =&gt; 'secret',
              'facebook_id' =&gt; null,
              'type' =&gt; 'customer',
              'confirmation_code' =&gt; null,
              'newsletter_subscription' =&gt; 0,
              'last_online' =&gt; null,
              'telephone' =&gt; null,
              'mobile' =&gt; null,
              'social_security_id' =&gt; null,
              'address_1' =&gt; null,
              'address_2' =&gt; null,
              'city' =&gt; null,
              'zip_code' =&gt; null,
              'signed_agreement' =&gt; 0,
              'is_email_confirmed' =&gt; 0
          ]);
  
          $user = User::find(1);
          $shop = new Shop([
              'id' =&gt; 1,
              'user_id' =&gt; $user-&gt;id,
              'name' =&gt; 'PureFoods Hotdog2',
              'description' =&gt; 'Something that describes this shop',
              'url' =&gt; null,
              'currency' =&gt; 'USD'
          ]);
          $user-&gt;shops()-&gt;save($shop);
  
          $shop = new Shop([
              'id' =&gt; 2,
              'user_id' =&gt; $user-&gt;id,
              'name' =&gt; 'PureFoods Hotdog',
              'description' =&gt; 'Something that describes this shop',
              'url' =&gt; null,
              'currency' =&gt; 'USD'
          ]);
  
          $user-&gt;shops()-&gt;save($shop);
  
          $this-&gt;user = $user;
  
      }
  
      /** @test */
      public function a_user_has_an_id(){
          $user =  User::find(1);
          $this-&gt;assertEquals(1, $user-&gt;id);
      }
  
      /** @test */
      public function a_user_has_a_first_name()
      {
          $this-&gt;assertEquals(&quot;John&quot;, $this-&gt;user-&gt;first_name);
      }
  
      /** @test */
      public function a_user_can_own_multiple_shops()
      {
          $shops = User::find(1)-&gt;shops()-&gt;get();
          var_dump($this-&gt;shops);
          $this-&gt;assertCount(2, $shops);
      }
  }
  </code></pre>
  <p>It seems, this error is caused by this line of code:
  <code>$user-&gt;shops()-&gt;save($shop);</code> - this code actually works when run in my sample routes or Controller but is throwing <code>errors</code> when run in <code>phpunit</code></p>
  <p><strong>User.php</strong></p>
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Notifications\Notifiable;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  
  class User extends Authenticatable
  {
      use Notifiable;
  
      protected $guarded = [ 'id' ];
      protected $table = &quot;users&quot;;   
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'password', 'remember_token',
      ];
  
  
      /**
       * returns the Shops that this user owned
       */
      public function shops()
      {
          return $this-&gt;hasMany('App\Shop');
      }
  }
  </code></pre>
  <p><strong>Shop.php</strong></p>
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Shop extends Model
  {
      protected $guarded = [ 'id' ];
      protected $table = &quot;shops&quot;;
  
      /**
       * returns the Owner of this Shop
       */
      public function owner()
      {
          return $this-&gt;belongsTo('App\User');
      }
  }
  </code></pre>
  <p>Any help will be very much appreciated. Thanks!</p>