繁体   English   中英

PHPUnit测试-无法断言实际大小11935与预期大小3相匹配

[英]PHPUnit test - Failed asserting that actual size 11935 matches expected size 3

我是phpunit的新手,并尝试编写一个断言创建了三个注释的测试,但是我从数据库中获取了所有注释。

    /** @test */
    public function it_gets_notes()
    {
        $address = Address::first();
        $notes = factory(AddressNote::class, 3)->create(['address_id' 
        => $address->id]);
        $found = $this->notesClass->getData(['address_id' => $address-
        >id]);
        $this->assertCount(3, $found);
    }
}

Address和AddressNote模型工作正常。 我认为我对getData方法最困惑,我知道我需要覆盖我的代码。 有人看到我所缺少的内容会在标题中产生错误吗?

如果在运行create方法后需要检查差异 ,则在添加它们之前和之后保存$found ,减号将是您的数字:

public function it_gets_notes()
{
    $address = Address::first();
    $found = $this->notesClass->getData(['address_id' => $address->id]);
    $notes = factory(AddressNote::class, 3)->create(['address_id' => $address->id]);
    $foundAfter = $this->notesClass->getData(['address_id' => $address->id]);
    $difference = count($foundAfter) - count($found);
    $this->assertEquals(3, $difference);
}

请注意,您需要将assertEquals()与3和现在的差异一起使用,而不是assertCount() ,因为您正在比较数字。

我不了解您的全部故事,但是我认为您的第一个错误是您没有创建测试数据库。
因此,这将是第一步-除了您的databaseName(无论名称如何)之外,还创建一个databaseName_test。
还有一些其他步骤-在.env文件中,将databaseName的名称更改为databaseName_testing-但仅在进行测试时(然后立即回滚到原始的databaseName)。
但是,问题仍然存在(PHP单元不是完美的工具,就像PHP一样),这是可以提供帮助的技巧。
代替:

    $this->assertEquals(3, $difference);

写:

    $this->assertEquals(11935, $difference); //the number is specific for your case

是的,这很愚蠢,但应该可以...

暂无
暂无

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

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