Php Can';我不明白这一点;拒绝重复用户';测验
考虑一个存储用户信息的简单类:Php Can';我不明白这一点;拒绝重复用户';测验,php,phpunit,Php,Phpunit,考虑一个存储用户信息的简单类: <?php class UserStore { private $users = array(); function addUser($name, $mail, $pass) { if (isset($this->users['mail'])) { throw new Exception("User {$mail} already in system.\n"); }
<?php
class UserStore {
private $users = array();
function addUser($name, $mail, $pass) {
if (isset($this->users['mail'])) {
throw new Exception("User {$mail} already in system.\n");
}
if (strlen($pass) < 5) {
throw new Exception("Password must have 5 or more letters.");
}
$this->users[$mail] =
array(
'pass' => $pass,
'mail' => $mail,
'name' => $name,
);
return true;
}
function notifyPasswordFailure($mail) {
if(isset($this->users[$mail])) {
$this->users[$mail]['failed'] = time();
}
}
function getUser($mail) {
return $this->users[$mail];
}
}
WTF?考试失败了!怎样查看测试输出,我看到一个名为Bill的数组。这怎么可能?在我看来,Bill从未被添加到用户中,因为抛出了异常,那么为什么我们在输出中看到它呢?要么我在理解PHPUnit/这个例子时犯了错误,要么这本书的例子是错误的。请帮忙 您的
addUser
方法中有一个输入错误-应该是
if (isset($this->users[$mail])) {
throw new Exception("User {$mail} already in system.\n");
}
顺便说一句,我认为这是一个糟糕的测试,因为你甚至无法从第一眼看出问题所在:)谢谢,谢谢,谢谢!我无法告诉你我经历了什么样的心理地狱才明白到底出了什么问题。对于学习PHP单元测试,您推荐什么?不是一个简单的问题:)我从来没有读过任何关于测试的书,但很多人推荐Kent Beck的《测试驱动开发:通过示例》一书。这里有一个伟大文献的列表(当我在寻找新的阅读材料时,我先到这里,然后是TDD示例)。另外,我建议将您的代码发布到。有一个很大的机会,有人会指出一些错误与良好的解释和“如何做得更好”。天哪!非常感谢您的链接和Codereview。我不知道SE在做这么酷的事!
1) UserStoreTest::testAddUserDuplicate
Failed asserting that Array (
'pass' => '123456'
'mail' => 'a@b.com'
'name' => 'Bill'
) does not contain 'Bill' and is of type "array".
if (isset($this->users[$mail])) {
throw new Exception("User {$mail} already in system.\n");
}