【问题标题】:Laravel Unit Testing InconsistencyLaravel 单元测试不一致
【发布时间】: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


【解决方案1】:

虽然这可能需要额外的工作,而且我无法准确告诉您为什么会发生这种情况,但您可能希望显式调用外部数组中的属性,如下所示:

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

我认为它这样做的原因是因为它首先创建模型对象,然后尝试访问查询搜索,并且无论出于何种原因它只是返回对象实例。在给出一个好的结论之前,我需要多读一点。我要说的是,它可能只是无法通过查询找到任何东西,因此返回了对象本身,但我实际上从未以这种方式使用过模型搜索,所以我不能准确地说这就是发生的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 2016-08-26
    • 2020-09-03
    • 2017-10-26
    • 2021-11-16
    相关资源
    最近更新 更多