teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

一部変更すると別のエラーが表示された

2023/01/31 23:18

投稿

oooz
oooz

スコア9

title CHANGED
File without changes
body CHANGED
@@ -71,15 +71,43 @@
71
71
  ];
72
72
  }
73
73
  ```
74
+ ### 補足情報(FW/ツールのバージョンなど)
74
75
 
76
+ laravel 9
77
+ mysql
78
+ phpunitは元々laravel9で入っていたものを使用
75
79
 
80
+ ### 追記
76
81
 
82
+ 下記サイトを参考にしデータベースを作成しましたが、テストを実行すると以下のエラーが表示されます。
83
+ 参考サイト
84
+ https://qiita.com/miriwo/items/cf07098590fa23ad24f1
77
85
 
86
+ ```
87
+ 1) Tests\Feature\userTest::test_list_access
88
+ Illuminate\View\ViewException: Attempt to read property "role" on null (View: /Applications/MAMP/htdocs/ファイル名/resources/views/list.blade.php)
89
+ ```
90
+ 直訳すると「データベースの”role”がnullです」となりますが、一応マイグレーションファイルや「database/factories/UserFactory.php」には情報を記載しているのですが、どちらに記載すればエラーは解消されるでしょうか?
78
91
 
79
-
92
+ ```テストでないマイグレーション
80
-
93
+ Schema::create('users', function (Blueprint $table) {
94
+ // $table->id();
95
+ $table->id();
96
+ $table->string('name', 10);
97
+ $table->string('email');
98
+ $table->string('password');
99
+ $table->string('img')->nullable();
100
+ $table->string('role')->default('11');
101
+ $table->timestamp('created_at')->useCurrent();
102
+ $table->timestamp('updated_at')->nullable();
103
+ });
104
+ ```
105
+ ```database/factories/UserFactory.php
106
+ return [
107
+ 'name' => $this->faker->name(),
108
+ 'email' => $this->faker->unique()->safeEmail(),
109
+ 'password' => '$2y$10$rUBiykZ1QwGMPS.uctohCugP4Zv0SSlYXs2eDQ98xa5vc2prf3yOm',
110
+ 'img' => 'images/***.jpg',
81
- ### 補足情報(FW/ツールのバージョンなど)
111
+ 'role' => 11,
82
-
83
- laravel 9
112
+ ];
84
- mysql
113
+ ```
85
- phpunitは元々laravel9で入っていたものを使用