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';
}
}
テストデータベースを設定しましたか? – Sandeesh
はい、テストデータベースが使用されています:メモリ:他のdbテストはうまく動作し、行: 'name' => $ account-> name(post内)は真(それが挿入されている)をアサートします – LeeJ
AccountApiKeyモデルと構造完全なテストと一緒に。 – Sandeesh