【发布时间】:2018-09-15 09:32:40
【问题描述】:
您好,我正在学习 laracast 的 laravel 课程,但在学习过程中遇到了一些困难。
在应用程序中,有一个带有返回分组记录的静态函数的 Post Model:
Post.php
public static function archives()
{
return static::selectRaw('year(created_at) as year, monthname(created_at) month, count(*) published')
->groupBy('year', 'month')
->orderByRaw('min(created_at) desc')
->get()
->toArray();
}
另一方面,测试用例旨在测试 Post::archives() 函数:
ExampleTest.php
public function testBasicTest()
{
// GIVEN I have two records in the database that are posts,
// and each one is posted a month apart.
$first = factory(Post::class)->create();
$second = factory(Post::class)->create([
'created_at' => \Carbon\Carbon::now()->subMonth()
]);
// WHEN I fetch the archives
$posts = \App\Post::archives();
// dd($posts);
// THEN the response should be in the proper format
$this->assertEquals([
[
"year" => $first->created_at->format('Y'),
"month" => $first->created_at->format('F'),
"published" => 1
],
[
"year" => $first->created_at->format('Y'),
"month" => $first->created_at->format('F'),
"published" => 1
],
], $posts);
}
但是,断言失败了,因为 $posts 是一个对象数组而不是一个数组数组,即使 Post::archives() 专门用 toArray() 转换了结果。
把它扔到修补匠上:
$posts = \App\Post::archives()
[
[
"year" => 2014,
"month" => "March",
"published" => 2,
],
[
"year" => 2014,
"month" => "February",
"published" => 1,
],
[
"year" => 2013,
"month" => "February",
"published" => 1,
],
]
phpunit的错误
1) Tests\Unit\ExampleTest::testBasicTest
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
Array (
- 0 => Array (...)
- 1 => Array (...)
+ 0 => App\Post Object (...)
+ 1 => App\Post Object (...)
)
dd($posts) 派生:-
..array:2 [
0 => App\Post {#863
#guarded: []
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:3 [
"year" => 2018
"month" => "April"
"published" => 1
]
#original: array:3 [
"year" => 2018
"month" => "April"
"published" => 1
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
1 => App\Post {#864
#guarded: []
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:3 [
"year" => 2018
"month" => "March"
"published" => 1
]
#original: array:3 [
"year" => 2018
"month" => "March"
"published" => 1
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
]
为什么它在 TestCase 中表现得如此?
【问题讨论】:
-
不,事实并非如此 assertEquals 意味着即使行的顺序必须相同,它也必须相同
-
@bader 我只是证明 Post::archives() 正在返回一个数组数组。为了清楚起见,我将包括实际的测试失败。
-
你在测试什么?您似乎正在尝试测试返回的数组是否相同,但实际上并非如此。
-
我认为您使用的是旧版本的 Laravel 我已经在 5.6 上对其进行了测试并获得了数组列表
-
@SteveChamaillard 我正在学习 laracast 的课程。我知道它们是不同的。因为在测试用例外部调用时, Post::archives() 返回数组数组;在测试用例中调用时, Post::archives() 返回对象数组。我想知道为什么
标签: php laravel unit-testing laravel-5