2 单元测试控制器,用于验证操作是否创建了新记录


2 Unit test controllers to verify that action created new record

当我对成员控制器的注册操作进行单元测试时,如何断言在我的成员测试数据库中创建了新记录?

我正在尝试使用 getLastInsertID,但我不知道如何加载成员模型来进行调用。

我已经对我的会员模型进行了测试,其中包括注册和激活,但是当我完成激活过程时出现错误。 我想在控制器中使用一个测试来验证是否可以注册新成员,然后使用两个测试激活新成员。

更新我在控制器测试中添加了App::uses('Member', 'Model');,并且还

  public function setUp() {
    parent::setUp();
    $this->Member = ClassRegistry::init('Member');
  }

在功能测试注册我有:

   $data = array(
      'Member' => array(
        'first_name' => 'Foo',
        'last_name' => 'Bar',
        'email' => 'foobar@example.org',
        'password' => 'password',
        'password_confirmation' => 'password',
        'security_question' => 0, // Your father's middle name?
        'security_answer' => 'Randolf the great',
      ),
    );
    $result = $this->testAction(
      '/Members/register',
      array('data' => $data, 'method' => 'post')
    );
    debug($result);
    $this->assertTrue($this->Member->validationErrors);
    $this->assertTrue($this->Member->getLastInsertId());

断言显示"未能断言 array() 为真"和"未能断言 null 为真"。

我能够在$this->vars中找到足够的信息来查找我的模型的记录。 现在一切都在测试中。