无法理解这个 'deny duplicate users' 测试
Can't understand this 'deny duplicate users' test
考虑一个存储用户信息的简单class:
<?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];
}
}
这是我们的测试用例,以确保 class 不会出现重复的电子邮件 ID:
<?php
class UserStoreTest extends PHPUnit_Framework_TestCase {
private $store;
public function setUp() {
$this->store = new UserStore();
}
public function tearDown() {
}
public function testAddUserDuplicate() {
try {
$ret = $this->store->addUser("Bob", "a@b.com", "123456");
$ret = $this->store->addUser("Bill", "a@b.com", "123456");
self::fail('Exception should\'ve been thrown.');
} catch (Exception $e) {
$const = $this->logicalAnd(
$this->logicalNot($this->contains("Bill")),
$this->isType('array')
);
self::AssertThat($this->store->getUser("a@b.com"), $const);
}
}
}
这个例子摘自一本书。逻辑看起来很简单:一旦在添加重复用户时抛出异常,我们确保 getUser()
不会给第二个用户。所以我 运行 这个测试并得到以下错误:
有 1 次失败:
1) UserStoreTest::testAddUserDuplicate
Failed asserting that Array (
'pass' => '123456'
'mail' => 'a@b.com'
'name' => 'Bill'
) does not contain 'Bill' and is of type "array".
卧槽?测试失败!如何?查看测试输出,我看到一个名为 Bill 的数组。这怎么可能?在我看来,Bill 从未被添加到用户中,因为抛出了异常,那么为什么我们会在输出中看到它?要么是我对PHPUnit/这个例子的理解有误,要么是书上的例子是错误的。请帮忙!
您的 addUser
方法有错字 - 应该是
if (isset($this->users[$mail])) {
throw new Exception("User {$mail} already in system.\n");
}
顺便说一句,我认为这是一个糟糕的测试,因为你甚至无法从第一眼看到哪里出了问题:)
考虑一个存储用户信息的简单class:
<?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];
}
}
这是我们的测试用例,以确保 class 不会出现重复的电子邮件 ID:
<?php
class UserStoreTest extends PHPUnit_Framework_TestCase {
private $store;
public function setUp() {
$this->store = new UserStore();
}
public function tearDown() {
}
public function testAddUserDuplicate() {
try {
$ret = $this->store->addUser("Bob", "a@b.com", "123456");
$ret = $this->store->addUser("Bill", "a@b.com", "123456");
self::fail('Exception should\'ve been thrown.');
} catch (Exception $e) {
$const = $this->logicalAnd(
$this->logicalNot($this->contains("Bill")),
$this->isType('array')
);
self::AssertThat($this->store->getUser("a@b.com"), $const);
}
}
}
这个例子摘自一本书。逻辑看起来很简单:一旦在添加重复用户时抛出异常,我们确保 getUser()
不会给第二个用户。所以我 运行 这个测试并得到以下错误:
有 1 次失败:
1) UserStoreTest::testAddUserDuplicate
Failed asserting that Array (
'pass' => '123456'
'mail' => 'a@b.com'
'name' => 'Bill'
) does not contain 'Bill' and is of type "array".
卧槽?测试失败!如何?查看测试输出,我看到一个名为 Bill 的数组。这怎么可能?在我看来,Bill 从未被添加到用户中,因为抛出了异常,那么为什么我们会在输出中看到它?要么是我对PHPUnit/这个例子的理解有误,要么是书上的例子是错误的。请帮忙!
您的 addUser
方法有错字 - 应该是
if (isset($this->users[$mail])) {
throw new Exception("User {$mail} already in system.\n");
}
顺便说一句,我认为这是一个糟糕的测试,因为你甚至无法从第一眼看到哪里出了问题:)