你能在 phpunit 的一个测试函数中有两个模型实例吗?


Can you have two instances of a model in one test function in phpunit?

我正在尝试测试无法插入重复的用户。 为此,我正在创建 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;
}

检查验证帐户已经存在异常的方式

也许你应该改变电子邮件

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

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

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

也许这应该是"''模型''用户已经存在异常"

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