前提・実現したいこと
Laravel8のFeatureでレコード追加件数確認のテストを通したい。
発生している問題・エラーメッセージ
StockTest.phpの//レコード件数確認
下にテストを実装し、テストを実行すると下記エラーが発生します。
(エラーで指摘された94行目のコードはコメントアウトにて記載)
お手数ですが、テストの記述等に怪しい点があればご指摘、アドバイスお願い致します。
root@75d06dc082fc:/work# vendor/bin/phpunit tests/Feature/StockTest.php PHPUnit 9.5.4 by Sebastian Bergmann and contributors. ........E 9 / 9 (100%) Time: 00:02.951, Memory: 32.00 MB There was 1 error: 1) Tests\Feature\StockTest::testInsertFactoryTest Error: Call to undefined function Tests\Feature\factory() /work/tests/Feature/StockTest.php:94 ERRORS! Tests: 9, Assertions: 17, Errors: 1.
該当のソースコード
StockTest.php
<?php namespace Tests\Feature; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; use App\Models\Stock; use App\Models\User; class StockTest extends TestCase { /** * A basic feature test example. * * @return void */ use RefreshDatabase; public function test_login_screen_can_be_rendered() { $response = $this->get('/login'); $response->assertStatus(200); } public function test_list_screen_can_be_rendered() { $response = $this->get('/list'); $response->assertStatus(302); } public function test_Can_Login(): void { $user = User::factory(User::class)->create([ 'password' => bcrypt('password'), ]); $response = $this->from('/login')->post('/login', [ 'email' => $user->email, 'password' => 'password', ]); $response->assertRedirect('/list'); $this->assertAuthenticatedAs($user); } public function test_add_screen_can_be_rendered() { $response = $this->get('/list/add'); $response->assertStatus(302); $response = $this->post('/list/addCheck'); $response->assertStatus(302); $response = $this->post('/list/addDone'); $response->assertStatus(302); } public function test_show_screen_can_be_rendered() { $response = $this->get('/list/show/{id}'); $response->assertStatus(302); } public function test_edit_screen_can_be_rendered() { $response = $this->get('/list/edit/{id}'); $response->assertStatus(302); $response = $this->post('/list/editCheck/{id}'); $response->assertStatus(302); $response = $this->post('/list/editDone/{id}'); $response->assertStatus(302); } public function test_del_screen_can_be_rendered() { $response = $this->get('/list/delCheck/{id}'); $response->assertStatus(302); $response = $this->post('/list/delDone/{id}'); $response->assertStatus(302); } public function test_search_screen_can_be_rendered() { $response = $this->post('/list/search'); $response->assertStatus(302); } //レコード件数確認 public function testInsertFactoryTest() { $stocks = Stock::factory(Stock::class, 3)->create(); //94行目エラーで指摘 $count = count($stocks); $this->assertEquals(3, $count); } }
app/Models/Stock.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Stock extends Model { use HasFactory; protected $fillable = ['shop','purchase_date','deadline','name','price','number']; }
database/factories/StockFactory.php
<?php namespace Database\Factories; use App\Models\Stock; use Illuminate\Database\Eloquent\Factories\Factory; class StockFactory extends Factory { protected $model = Stock::class; public function definition() { return [ 'shop' => $this->faker->word, 'purchase_date' => $this->faker->dateTimeBetween('-20 days', now()), 'deadline' => $this->faker->dateTimeBetween(now(), '+1 week'), 'name' => $this->faker->word, 'price' => $this->faker->numberBetween(10,100), 'number' => $this->faker->numberBetween(10,100) ]; } }
試したこと
・テストの記述を変更し、再度テスト実行
public function testInsertFactoryTest(): void { $stocks = Stock::factory(Stock::class, 3)->create([ //'shop' => 'ドンキ', //'purchase_date' => '2020-09-12', //'deadline' => '2020-10-12', //'name' => 'ティッシュ', //'price' => '200', //'number' => '2' ]); $count = count($stocks); $this->assertEquals(3, $count); }
public function testInsertFactoryTest(): void { $stocks = Stock::factory(Stock::class, 3)->create([ 'shop' => $this->faker->word, 'purchase_date' => $this->faker->dateTimeBetween('-20 days', now()), 'deadline' => $this->faker->dateTimeBetween(now(), '+1 week'), 'name' => $this->faker->word, 'price' => $this->faker->numberBetween(10,100), 'number' => $this->faker->numberBetween(10,100) ]); $count = count($stocks); $this->assertEquals(3, $count); }
補足情報(FW/ツールのバージョンなど)
回答1件
あなたの回答
tips
プレビュー