Laravel unit tests - changing the configuration value depending on the testing method

I have an application with a system for checking accounts (register → receive an email with an activation link → verified account). This validation flow is optional and can be disabled using the configuration value:

// config/auth.php return [ // ... 'enable_verification' => true ]; 

I want to test the registration controller:

  • he should redirect to the main page in both cases
  • when verification is enabled, the home page should display a "sent by email" message
  • when verification is off, the message 'account created' should be displayed on the main page
  • and etc.

My testing methods:

 public function test_UserProperlyCreated_WithVerificationDisabled() { $this->app['config']->set('auth.verification.enabled', false); $this ->visit(route('frontend.auth.register.form')) ->type('Test', 'name') ->type(' test@example.com ', 'email') ->type('123123', 'password') ->type('123123', 'password_confirmation') ->press('Register'); $this ->seePageIs('/') ->see(trans('auth.registration.complete')); } public function test_UserProperlyCreated_WithVerificationEnabled() { $this->app['config']->set('auth.verification.enabled', true); $this ->visit(route('frontend.auth.register.form')) ->type('Test', 'name') ->type(' test@example.com ', 'email') ->type('123123', 'password') ->type('123123', 'password_confirmation') ->press('Register'); $this ->seePageIs('/') ->see(trans('auth.registration.needs_verification')); } 

During debugging, I noticed that the configuration value, when the value in the configuration file is always set inside the controller method, regardless of what I set with $this->app['config']->set...

I have other tests in the user repository itself to verify that it works when validation is on or off. And there, the tests behave as expected.

Any idea why this fails for the controllers and how to fix it?

+6
source share
1 answer

If I understand your question correctly - you are looking for "how to set the configuration parameter", then you can use Config::set($key, $value)

+4
source

All Articles