在本节开头运行全部测试没通过的原因
如果你是照着教程一步一步走到这的,那么当你运行
$ phpunit
时,很有可能会出现一个错误:
...
1) Tests\Feature\ViewQuestionsTest::user_can_view_a_single_question
Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Models\Question] 1
...
如果你是边做边思考的话,不难知道这个错误的原因,因为上一节我们修改了 QuestionsController 的 show 方法,如果没有发布时间(published_at)的 question 不会被显示出来。
而出错的这个测试,我们在创建测试数据时是没有添加 published_at 的。所以无法通过测试。
可以直接删掉 user_can_view_a_single_question 方法
因为如果要在创建测试数据的时候添加 发布日期,则和 user_can_view_a_published_question() 方法重复了
关于 LearnKu
推荐文章: