Laravel 5 Unit Test - Call to a member function connection() on null

前端 未结 7 1322
灰色年华
灰色年华 2020-12-31 06:53

I tried creating a unit test for the relationships between my User and Shop models, however when I run vendor\\\\bin\\\\phpunit this e

7条回答
  •  灰色年华
    2020-12-31 07:39

    That can be happens when you try to read database from dataprovider function. Like me tried, yes. The work way:

    protected static $tariffs_default;
    protected function setUp(): void {
        parent::setUp ();
        if (!static::$tariffs_default){
            static::$tariffs_default = DB::...;
        }
    }    
    // in dataprovider we use string parm, named as our static vars
    public static function provider_prov2(){
        return [
            [".....", [ 'tariffs'=>'tariffs_default'] ],
        ];
    }
    // then, in test we can ask our data by code:
    public function testSome(string $inn, string $ogrn, $resp_body){
        if ( $ta_var = Arr::get( $resp_body, 'tariffs' ) ){
            Arr::set($resp_body, 'tariffs', static::$$ta_var );
        }
        $this->assert...
    }
    

提交回复
热议问题