Laravel 单元测试 - 根据测试方法更改配置值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37862409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:00:01  来源:igfitidea点击:

Laravel unit tests - changing a config value depending on test method

phpunit-testinglaravelphpunit

提问by Vincent Mimoun-Prat

I have an application with a system to verify accounts (register -> get email with link to activate -> account verified). That verification flow is optional and can be switched off with a configuration value:

我有一个带有系统验证帐户的应用程序(注册 -> 获取带有激活链接的电子邮件 -> 帐户已验证)。该验证流程是可选的,可以使用配置值关闭:

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

I want to test the registration controller:

我想测试注册控制器:

  • it should redirect to home page in both cases
  • when verification is ON, home page should show message 'email sent'
  • when verification is OFF, home page should show message 'account created'
  • etc.
  • 在这两种情况下它都应该重定向到主页
  • 当验证打开时,主页应显示消息“电子邮件已发送”
  • 当验证关闭时,主页应显示消息“帐户已创建”
  • 等等。

My test 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('[email protected]', '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('[email protected]', 'email')
        ->type('123123', 'password')
        ->type('123123', 'password_confirmation')
        ->press('Register');

    $this
        ->seePageIs('/')
        ->see(trans('auth.registration.needs_verification'));
}

When debugging, I noticed that the configuration value when inside the controller method is always set to the value in the config file, no matter what I set with my $this->app['config']->set...

调试时,我注意到控制器方法内部的配置值始终设置为配置文件中的值,无论我用我的 $this->app['config']->set...

I have other tests on the user repository itself to check that it works both when validation is ON or OFF. And there the tests behave as expected.

我对用户存储库本身进行了其他测试,以检查它在验证打开或关闭时是否有效。在那里,测试的行为符合预期。

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

知道为什么控制器失败以及如何解决这个问题吗?

回答by Alex Slipknot

If?I understand your question right - you looking for "how to set config param", then you can?use Config::set($key, $value)

如果?我理解你的问题——你在寻找“如何设置配置参数”,那么你可以使用 Config::set($key, $value)

回答by Eduard Brokan

Correct answer is above

正确答案在上面

Config::set($key, $value) 

Example

例子

//Not forget to add namespace using class.
use Config;

//Example to set config. (configFile.key)
Config::set('auth.enable_verification', false);