CakePHP - 基本单元测试失败,但应用程序工作?

时间:2011-07-20 21:41:23

标签: unit-testing testing cakephp cakephp-1.3

因为我已经在这个问题上抓了一个小时 - 我猜它的东西很小,而且我很蠢。基本上我正在为我的应用程序测试一个模型方法,它在大约2个小时前工作 - 然后我添加了一些更多的测试,他们一直都失败了,因为即使我回到原来的基本版本:

我的夹具:

class UserFixture extends CakeTestFixture {
    var $name = 'User';
}
var $fields = array(
    'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
    'group_id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'index'),
    'username' => array('type' => 'string', 'null' => false, 'length' => 16, 'key' => 'unique', 'collate' => 'utf8_unicode_ci', 'charset' => 'utf8'),
    'password' => array('type' => 'string', 'null' => false, 'length' => 64, 'collate' => 'utf8_unicode_ci', 'charset' => 'utf8'),
    'email' => array('type' => 'string', 'null' => false, 'length' => 128, 'key' => 'unique', 'collate' => 'utf8_unicode_ci', 'charset' => 'utf8'),
    'status' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'length' => 1),
    'created' => array('type' => 'datetime', 'null' => true, 'default' => NULL),
    'modified' => array('type' => 'datetime', 'null' => true, 'default' => NULL),
    'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1), 'username' => array('column' => 'username', 'unique' => 1), 'email' => array('column' => 'email', 'unique' => 1), 'group_id' => array('column' => 'group_id', 'unique' => 0)),
    'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_unicode_ci', 'engine' => 'InnoDB')
);

我的测试:

function startTest() {
    $this->User =& ClassRegistry::init('User');
}

function endTest() {
    unset($this->User);
    ClassRegistry::flush();
}

function testNewUser(){
    $data = array('User' => array('username' => 'johndoe', 'password' => 'john', 'email' => 'john@doe.com'));

    $result = $this->User->newUser($data);
    $expected = array(
        'User' => array(
            'id' => 1,
            'group_id' => 3,
            'username' => 'johndoe',
            'password' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //replaced hashed password with x's for posting
            'email' => 'john@doe.com',
            'status' => 0,
            'created' => date('Y-m-d H:i:s'),
            'modified' => date('Y-m-d H:i:s'),
        )
    );
    $this->assertEqual($result, $expected);
}

我试图测试的模型方法:

function newUser($data, $group = null){
    $this->create();
    $user['User']['username'] = $data['User']['username'];
    $user['User']['password'] = $data['User']['password'];
    $user['User']['email'] = $data['User']['email'];
    $user['User']['status'] = 0;
    $group = empty($group) ? 3 : $group;
    $user['User']['group_id'] = $group;
    //return true;
    return $this->save($user);
}

错误:

FAILED Equal expectation fails as [Boolean: false] does not match [Array: 1 items] 
at [/Users/shaz/Sites/cakeapp/tests/cases/models/user.test.php line 36] 
/Users/shaz/Sites/cakeapp/tests/cases/models/user.test.php -> UserTestCase -> testNewUser

我假设,这是假的,来自$ this-> save($ user)实际上没有保存并返回false;而且我不确定为什么它可以通过控制器在应用程序中运行($ this->来自表单的数据)。

0 个答案:

没有答案