0
我有一個簡單的方法get_db_password()
它可以返回一個字符串,或者在某些情況下它可能會返回null
,這兩種情況都應該被認爲是有效的響應。
我真正測試的是如果調用getter,腳本不會爆炸。
如何編寫測試/斷言 - 要麼調用get_db_password()
,要麼聲明腳本沒有死亡,要麼測試響應是否爲null
或字符串。 e.g像如何在phpunit中測試多個可能的有效值?
$this->assertInternalType("string || null", $this->config->get_db_password());
的源代碼
<?php
class Config {
/** @var string $db_password stored the database password */
private $db_password;
public function __construct() {
$this->db_password = require(PROJ_DIR . "config/productions.php");
}
/** @return string
*/
public function get_db_password() {
return $this->db_password;
}
}
測試代碼
<?php
class ConfigTest extends PHPUnit\Framework\TestCase {
public $config;
public function test_get_db_password_returns_a_string_or_null() {
$this->config = new Config;
// how can I write this test?
$this->assertInternalType('string || null', $this->config->get_db_password());
}
}