php - Unit testing with the config app file in Laravel -


my model method relies on config() global, here;

public function getgroup() {     if(config('app.pages.'.$this->group.'.0')) {         return $this->group;     }     return "city"; } 

i trying test method in unit test class, here;

public function testgetgroupreturnscityasdefault() {     $response = new response();     $response->group = "town";     $test = $response->getgroup();     dd($test); } 

the error is;

error: call member function make() on null /home/vagrant/sites/vendor/laravel/framework/src/illuminate/foundation/helpers.php:62 /home/vagrant/sites/vendor/laravel/framework/src/illuminate/foundation/helpers.php:163 

i know related config() global. not sure how set in test. tried

public function setup() { config(['app.pages' => [     'city' => [........ 

but got same error. how can set up?

i'm not sure if have solved yet had similar issue.

there 2 things had work:

1) make sure calling setup parent method so:

public function setup() {     parent::setup();      // other stuff } 

2) make sure test class extending laravel testcase rather phpunit_framework_testcase

brief explanation: error getting because containerinstance of laravel null since going through phpunit , such never created. if above steps you'll ensure laravel first instantiate container instance.

p.s. if going end referencing env variables, should phpunit.xmlsection enviornment variables.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -