简体   繁体   English

Laravel单元测试不一致

[英]Laravel Unit Testing Inconsistency

Hi I am following the laravel course from laracast , but faced some difficulty following the course. 嗨,我正在学习laracast的laravel课程,但是在学习该课程时遇到了一些困难。

In the app, there is a Post Model with a static function that returns grouped records: 在应用程序中,有一个带有静态函数的Post Model,可以返回分组记录:

Post.php 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();
}

On the other hand, a test case aims to test the Post::archives() function: 另一方面,一个测试用例旨在测试Post :: archives()函数:

ExampleTest.php 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);
}

However, the assertion failed, because $posts is an array of object instead of an array of array, even though Post::archives() specifically converted the results with toArray() . 但是,断言失败了,因为$ posts是一个对象数组,而不是数组数组,即使Post::archives()专门使用toArray()转换了结果。

Dumping it on tinker: 将其倾倒在修补匠上:

$posts = \App\Post::archives()
[
 [
   "year" => 2014,
   "month" => "March",
   "published" => 2,
 ],
 [
   "year" => 2014,
   "month" => "February",
   "published" => 1,
 ],
 [
   "year" => 2013,
   "month" => "February",
   "published" => 1,
 ],
]

The error on phpunit 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) derives:- 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: []
  }
]

Why is it behaving as such in the TestCase? 为什么它在TestCase中表现得如此?

While it might be extra labor, and I can't exactly tell you why it happens, you might want to explicitly call the attributes in an outside array, as in: 尽管这可能是额外的工作,但我无法确切告诉您发生这种情况的原因,但您可能希望在外部数组中显式调用属性,如下所示:

$postsArray = [
    "year" => $posts->year,
    "month" => $posts->month,
    "published" => $posts->published
];

I think the reason it is doing that is because it first creates the model object and then attempts to access the query search, and for whatever reason it simply returns the object instance. 我认为这样做的原因是因为它首先创建了模型对象,然后尝试访问查询搜索,并且无论出于何种原因,它只是返回对象实例。 I would need to read a little more before giving a good conclusion. 在得出一个良好的结论之前,我需要阅读更多内容。 I was going to say that it might just be failing to find anything with the query and thus returning the object itself, but I haven't actually ever used a model search that way so I can't say with precision that's what happens. 我要说的是,可能无法在查询中找到任何内容,从而返回对象本身,但是我实际上从未以这种方式使用过模型搜索,因此我无法精确地说出发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM