PHPUnit assertDatabaseHas()返回TypeError [英] PHPUnit assertDatabaseHas() returning TypeError
问题描述
试图在这里测试模型,但是每次我尝试使用assertDatabaseHas方法时,都会给我类型错误. Laravel 5.4和PHPUnit 6.
Trying to test a model here, but every time that I try to use the assertDatabaseHas method it gives me the type error. Laravel 5.4 and PHPUnit 6.
PHPUnit 6.1.4 by Sebastian Bergmann and contributors.
..E 3 / 3 (100%)
Time: 815 ms, Memory: 10.00MB
There was 1 error:
1) Tests\Unit\ModelTest::test_model_creation
TypeError: Argument 2 passed to PHPUnit\Framework\Assert::assertThat() must be an instance of PHPUnit\Framework\Constraint\Constraint, instance of Illuminate\Foundation\Testing\Constraints\HasInDatabase given, called in /home/vagrant/domains-api/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php on line 22
/home/vagrant/domains-api/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php:22
/home/vagrant/domains-api/tests/Unit/ModelTest.php:36
ERRORS!
Tests: 3, Assertions: 2, Errors: 1.
测试类中的代码:
$this->assertDatabaseHas('domains', [
'domain' => $domain,
'domain_type' => 'full_domain',
]);
我使用laravel文档作为参考,并且语法似乎是正确的(至少对我而言). https://laravel.com/docs/5.4/database-testing#introduction
I using the laravel docs as reference and the syntax seems to be correct (at least for me). https://laravel.com/docs/5.4/database-testing#introduction
在此先感谢您的帮助.
推荐答案
问题似乎是Laravel 5.4不支持版本6引入的PHPUnit的命名空间.
The problem seems to be that Laravel 5.4 does not support the namespace for PHPUnit introduced with version 6.
如果比较分支,很容易看到:
If you compare the branches it is easy to see:
vs.
我认为最好的解决方案是使用PHPUnit 5.x运行测试,或者可能更新laravel/framework
以使用master.由于主服务器可能包含重大更改,因此第一种似乎是目前较为安全的方法.
I think the best solution is to either run the tests with PHPUnit 5.x or possibly update laravel/framework
to use master. Since the master might contain breaking changes the first seems to be the safer approach for now.
这篇关于PHPUnit assertDatabaseHas()返回TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!