laravel - 使用 Laravel 5.4 和 phpunit 测试表单数组

标签 laravel phpunit

在以下示例中发布“AccountApiKey[merchant_id]”以测试 Laravel 5.4 中的插入的正确语法是什么。

在生产中,代码工作正常,没有任何问题,在测试中,我收到错误“找不到表”,因为它没有正确插入。我已经在 IRC 中询问过,但它难倒了他们,所以我希望 stackoverflow 的精彩社区能够提供帮助。

谢谢!

查看:

{{-- Merchant ID form input --}}
<div class="form-group">
    {!! Form::label('AccountApiKey[merchant_id]', 'Seller ID:') !!}
    {!! Form::text('AccountApiKey[merchant_id]', null, ['class' => 'form-control']) !!}
</div>

测试(完整测试请参阅下面的更新):

    $accountApiKey = factory(AccountApiKey::class)->make([
        'merchant_id' => 'test'
    ]);

$this->post($this->urlToUse(), [
            'name' => $account->name,
            'AccountApiKey[merchant_id]' => $accountApiKey->merchant_id,
]);



    $this->assertDatabaseHas('account_api_keys', [
        'merchant_id' => $accountApiKey->merchant_id,
]);

Controller :

   $account->accountApiKey()->save(new AccountApiKey($request->get('AccountApiKey')));

根据 Sandeesh 评论更新:

型号:

class AccountApiKey extends Model implements Transformable
{
    use TransformableTrait;

    protected $fillable = ['last_modified_user_id', 'merchant_id'];

    protected $events = ['saving' => SettingsUpdated::class];

    public function account()
    {
        return $this->belongsTo('App\Models\Account\Settings\Accounts');
    }

}

完成测试:

 class StoreTest extends TestCase implements TestInterface
{

    use DatabaseMigrations;

    /**
     * Tests all forms are inserting correct into database
     */
    public function test_inserting_into_database()
    {
        $user1 = $this->userAndCompany();

        $this->actingAs($user1);

        $account = factory(Account::class)->create([
            'company_id' => $user1->company()->first()->id,
        ]);

        $accountApiKey = factory(Account\AccountApiKey::class)->make([
            'last_modified_user' => $user1->id,
            'account_id' => $account->id,
            'merchant_id' => 'test'
        ]);


        $this->post($this->urlToUse(), [
            'name' => $account->name,
            'AccountApiKey[merchant_id]' => $accountApiKey->merchant_id,
            ]);

        $this->assertDatabaseHas('accounts', [
            'name' => $account->name, //asserts true
        ]);

        $this->assertDatabaseHas('account_api_keys', [
            'merchant_id' => $accountApiKey->merchant_id, // asserts false
        ]);

    }

        /**
     * The url which is under test
     * @return mixed
     */
    function urlToUse()
    {
        return 'account/settings/account';
    }

}

最佳答案

好的,就是这样,我发现了问题,您必须将帖子数据更改为此才能使测试正常工作。

$this->post($this->urlToUse(), [
    'name' => $account->name,
    'AccountApiKey' => [
        'merchant_id' => $accountApiKey->merchant_id,
    ],
]);

同时,您不必要地使用可以使用普通输入的数组输入。如果您愿意,我会给您一些清理代码的建议。

关于laravel - 使用 Laravel 5.4 和 phpunit 测试表单数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44053272/

相关文章:

php - Laravel Logging - 自定义 channel 不写

php - 运行时异常 : Unable to guess the Kernel directory

php - 类 __PHP_Incomplete_Class 没有反序列化器

html - 在 Laravel 4 中使用图标链接

php - 为什么 validate() 方法可以通过 request() 访问?

PhpUnit 与 Zend Framework 内存问题

phpUnit 无法识别 getenv/apache_getenv

database - 使用 phpUnit 进行 Laravel 5 测试而不重置我的数据库

php - Laravel - 将数据传递到电子邮件 View

ajax - Laravel session 未通过 AJAX 请求更新