0

我正在尝试测试不能插入重复的用户。为此,我创建了 2 个具有相同详细信息的用户对象,只是更改了帐户名称 - 因为这也是独一无二的。

但是,我的二传手似乎没有第二次设置公司名称。当我从模型中回显时,我尝试设置的属性仍然与我创建的前一个对象相同。我的测试失败,因为它抛出了我设置的帐户已存在异常

Failed asserting that exception of type "Models\AccountAlreadyExistsException" matches expected exception "Models\UserAlreadyExistsException".

public function testCantInsertDuplicateUser ()
{

    $user = new \Models\User();
    $user->first_name = 'John';
    $user->surname = 'Smith';
    $user->email = 'myemail@gmail.com';
    $user->password = 'password';
    $user->password_confirmation = 'password';
    $user->setCompanyName('Star');
    $user->setPackageId(2);
    $this->assertTrue($user->signUp());

    $user2 = new \Models\User();
    $user2->first_name = 'John';
    $user2->surname = 'Smith';
    $user2->email = 'myemail@gmail.com';
    $user2->password = 'password';
    $user2->password_confirmation = 'password';
    $user2->setCompanyName('cross');
    $user2->setPackageId(2);

    $this->setExpectedException('Models\UserAlreadyExistsException');
    $user2->signUp();
}

//user model
    public function setCompanyName ($company_name)
{
    $this->company_name = $company_name;
}

private function insertAccount ()
{
    $account = new \Models\Account;


    $account->setCompanyName($this->company_name);

    $account->setPackageId($this->package_id);

    $this->account_message_bag = new \Illuminate\Support\MessageBag();

    if (!$account->insert()) {

        $this->account_message_bag = $account->getValidationErrors();

    }

    return $account;
}

private function insertUser ()
{
    $save_user = $this->save(self::$rules, array(), array(), function ($model)
    {
        //this is all performed before save
        $existing_email = User::where('email', "=", $this->email)->count();

        if ($existing_email) {

            //delete account that was created in previous step
            //as the signup hasn't been successful

            $this->account->delete();

            throw new UserAlreadyExistsException();

        }

        //are there any account validation errors?

        if (count($this->account_message_bag->getMessages()>0)) {

            return false;
        }

    });

    return $save_user;
}

public function signUp ()
{
    $this->account = $this->insertAccount();

    $this->account_id = $this->account->getId();


    if (!$this->insertUser()) {

        //delete the company created in previous step

        $this->account->delete();

        $user_message_bag = Ardent::errors();

        //combine user and account validation eerrors

        $message_array = array_merge($user_message_bag->getMessages(), $this->account_message_bag->getMessages());

        $this->validation_errors = new \Illuminate\Support\MessageBag($message_array);

        throw new GenericValidationException();
    }

    //TODO - does this return false on failure?

    $sent = $this->sendConfirmEmail();

    if (!$sent) {

        throw new WelcomeEmailNotSent();
    }
    //sende confirm email
    return true;
}

//Account model

public function insert ()
{
    $result = $this->save(self::$rules, array(), array(), function()
    {
        $existing_company_name = \Models\Account::where('company_name', '=', $this->company_name)->count();

        if ($existing_company_name) {
            throw new AccountAlreadyExistsException();
        }
    });

    if (!$result) {


        $this->validation_errors = Ardent::errors();

        return false;
    }
    return true;
}
4

2 回答 2

0

检查您验证 AccountAlreadyExistsException 的方式

也许您应该更改电子邮件

于 2013-10-31T11:48:41.817 回答
0

您的名称空间可能是问题所在。

您在 \Models 中创建类,但使用 Models(不带反斜杠)作为例外。也许你的代码抛出\Models\UserAlreadyExsitsException,而不是Models\UserAlreadyExistsException。

$user2 = new \Models\User();
...
$this->setExpectedException('Models\UserAlreadyExistsException');

也许这应该是 '\Models\UserAlreadyExistsException'

$user2 = new \Models\User();
...
$this->setExpectedException('\Models\UserAlreadyExistsException');
于 2013-10-31T14:40:02.587 回答