【问题标题】:Laravel PHPUnit Call to a member function connection() on nullLaravel PHPUnit 在 null 上调用成员函数 connection()
【发布时间】:2019-01-12 16:40:23
【问题描述】:

我正在学习 Laravel 测试,但由于此错误,我无法继续测试 Eloquent:

错误:在 null 上调用成员函数 connection()

这是我的代码:

public function test_users_can_follow_each_other()
    {
        $this->userOne = User::find(1);
        $this->userTwo = User::find(2);
        $this->userOne->followedBy($this->userTwo->id);
        $this->assertTrue($this->userTwo->isFollowedBy($this->userOne->id));
    }

我也在使用 trait use DatabaseTransactions;

数据库已经创建,用户表上有 2 条记录。还需要配置什么吗,谢谢!

【问题讨论】:

  • 你说的那两条记录是怎么创造的?如果您运行测试,它通常会全部清除,您需要为每个测试设置数据库(记录)。
  • 首先我使用的是:$this->userOne = factory(User::class)->create(); $this->userTwo = factory(User::class)->create();但是我收到了这个错误: InvalidArgumentException: Unable to locate factory with name [default] [App\User].
  • 是的,这就是您为一项特定测试设置数据库的方式。所以在代码中添加这两行。也将其更改为factory(App\User::class, 2)->create()。
  • 我仍然遇到同样的错误:InvalidArgumentException: Unable to locate factory with name [default] [App\User]。
  • 这是什么 Laravel 版本?

标签: laravel phpunit


【解决方案1】:

替换

PHPUnit\Framework\TestCase

与

测试\TestCase

【讨论】:

    猜你喜欢
    • 2021-01-25
    • 1970-01-01
    • 2018-02-06
    • 2017-07-19
    • 1970-01-01
    • 2016-09-17
    • 2019-02-24
    • 1970-01-01
    相关资源
    最近更新 更多