douluan5444 2015-10-31 14:38
浏览 9
已采纳

无法理解这个'拒绝重复用户'的测试

Consider a simple class that stores the information of users:

<?php
class UserStore {
    private $users = array();

    function addUser($name, $mail, $pass) {
        if (isset($this->users['mail'])) {
            throw new Exception("User {$mail} already in system.
");
        }

        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];
    }
}

And here's our test case to ensure that the class doesn't expect duplicate email ids:

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

This example is taken from a book. The logic seems simple enough: Once an exception has been thrown on adding duplicate user, we ensure getUser() doesn't give the second user. So I run this test and get the following error:

There was 1 failure:

1) UserStoreTest::testAddUserDuplicate
Failed asserting that Array (
    'pass' => '123456'
    'mail' => 'a@b.com'
    'name' => 'Bill'
) does not contain 'Bill' and is of type "array".

WTF? The test failed! How? Looking at the test output, I see an array with name Bill. How is this possible? The way I see it, Bill was never added to users because an exception was thrown, then why do we see it in the output? Either I have made a mistake in understanding PHPUnit / this example, or the book's example is wrong. Please help!

  • 写回答

1条回答 默认 最新

  • dongyejun1983 2015-11-01 15:57
    关注

    There is a typo in your addUser method - should be

    if (isset($this->users[$mail])) {
        throw new Exception("User {$mail} already in system.
    ");
    }
    

    BTW, I think it's is a bad test, because you even can't get what is wrong from the first view:)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么