現在テストコードを書いているのですが、assertDatabaseHas()
を実行すると以下のエラーが出ます。
1) Tests\Unit\ArticleTest::testSend TypeError: Argument 2 passed to Illuminate\Foundation\Testing\TestCase::assertDatabaseHas() must be of the type array, object given, called in /var/www/tests/Unit/ArticleTest.php on line 24
原因を自分で調べたところ、型の指定エラーということがわかりました。
しかししっかりとテーブル名、入れたいデータの配列を指定しているので、エラーが出る原因がわかりません。
/* app/Article.php */ <?php namespace App; use Illuminate\Database\Eloquent\Model; class Article extends Common { protected $table = 'article'; protected $primaryKey = 'article_table_id'; protected $guarded = [ 'article_table_id', 'article_table_key', 'category_table_id', 'type_table_id', 'author_table_id', 'image_table_id', 'status', 'publish_at', ]; public function author(){ return $this->belongsTo('App\Author', 'author_table_id', 'author_table_id')->withDefault(); } public function category(){ return $this->belongsTo('App\Category', 'category_table_id', 'category_table_id')->withDefault(); } public function type(){ return $this->belongsTo('App\Type', 'type_table_id', 'type_table_id')->withDefault(); } public function tags(){ return $this->belongsToMany('App\Tag', 'article_tag', 'article_table_id', 'tag_table_id')->withTimestamps(); } public function images(){ return $this->belongsToMany('App\Image', 'article_image', 'article_table_id', 'image_table_id')->withTimestamps(); } public function thumbnail(){ return $this->belongsTo('App\Image', 'image_table_id', 'image_table_id'); } }
//モーダルファイル <?php use Faker\Generator as Faker; use Carbon\Carbon; $factory->define(App\Article::class, function (Faker $faker) { return [ 'article_table_id' => $faker->numberBetween(1, 18), 'article_table_key' => $faker->shuffle('abcdefghijklmnopqrstuvwxyz'), 'category_table_id' => $faker->numberBetween(10, 17), 'type_table_id' => $faker->numberBetween(1, 2), 'author_table_id' => $faker->numberBetween(8, 10), 'image_table_id' => $faker->numberBetween(1, 6), 'status' => $faker->shuffle('abcdefghijklmnopqrstuvwxyz'), 'publish_at' => $faker->dateTimeThisDecade(), ]; });
<?php namespace Tests\Unit; use Tests\TestCase; use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use App\Article; use Carbon\Carbon; class ArticleTest extends TestCase { use DatabaseTransactions; public function testSend() { //テーブル名指定 $table = 'article'; $article = factory('App\Article')->make(); //ここでのエラーが上記で表示してあるエラー文 $this->assertDatabaseHas('table', $article); $this->post('article/create', $article->toArray())->assertSessionHasErrors('publish_at'); } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。