2017-05-18 7 views
0

Laravel 5.4で挿入をテストするための下の例で 'AccountApiKey [merchant_id]'を投稿する正しい構文は何ですか?フォーム配列をLaravel 5.4とphpunitでテストする

コードは正常に動作しますが、テストで正しく挿入されていないエラーが「テーブルが見つかりません」というエラーが表示されます。私はIRCで尋ねましたが、スタックオーバーフローの素晴らしいコミュニティが助けてくれることを願っています。

ありがとうございます!

ビュー:

{{-- 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, 
]); 

コントローラー:Sandeeshコメントどおり

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

更新:

モデル:

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'; 
    } 

} 
+0

テストデータベースを設定しましたか? – Sandeesh

+0

はい、テストデータベースが使用されています:メモリ:他のdbテストはうまく動作し、行: 'name' => $ account-> name(post内)は真(それが挿入されている)をアサートします – LeeJ

+0

AccountApiKeyモデルと構造完全なテストと一緒に。 – Sandeesh

答えて

1

[OK]を、それは私が問題を発見し、あなたが仕事へのテストのためにこれにあなたのポストのデータを変更する必要があり、です。

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

同時に、通常の入力を使用できるアレイ入力を不必要に使用しています。あなたが望むなら、コードを少しきちんと整理する助言をします。

関連する問題