簡體   English   中英

Laravel 單元測試 - 根據測試方法更改配置值

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

我有一個帶有系統驗證帳戶的應用程序(注冊 -> 獲取帶有激活鏈接的電子郵件 -> 帳戶已驗證)。 該驗證流程是可選的,可以使用配置值關閉:

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

我想測試注冊控制器:

  • 在這兩種情況下它都應該重定向到主頁
  • 當驗證打開時,主頁應顯示消息“已發送電子郵件”
  • 當驗證關閉時,主頁應顯示消息“帳戶已創建”
  • 等。

我的測試方法:

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'));
}

調試時,我注意到控制器方法內部的配置值始終設置為配置文件中的值,無論我用$this->app['config']->set...

我對用戶存儲庫本身進行了其他測試,以檢查它在驗證打開或關閉時是否有效。 在那里,測試按預期運行。

知道為什么控制器失敗以及如何解決這個問題嗎?

如果我理解你的問題——你在尋找“如何設置配置參數”,那么你可以使用Config::set($key, $value)

正確答案在上面

Config::set($key, $value) 

示例

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM