【问题标题】:Imposible to use Factory in PHPUnit / Laravel 5.1无法在 PHPUnit / Laravel 5.1 中使用工厂
【发布时间】:2016-06-25 15:07:20
【问题描述】:

我正在尝试使用 PHPUnit / Integrated 进行功能测试。

所以,我需要生成一个假对象,并填写表单中的所有字段。

当我尝试使用 Faker 生成 Person 时,我收到以下错误消息:

PHP Fatal error:  Call to a member function make() on null in /home/vagrant/RH/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php on line 62
PHP Stack trace:
PHP   1. {main}() /home/vagrant/RH/vendor/phpunit/phpunit/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /home/vagrant/RH/vendor/phpunit/phpunit/phpunit:47
PHP   3. PHPUnit_TextUI_Command->run() /home/vagrant/RH/vendor/phpunit/phpunit/src/TextUI/Command.php:100
PHP   4. PHPUnit_TextUI_TestRunner->doRun() /home/vagrant/RH/vendor/phpunit/phpunit/src/TextUI/Command.php:149
PHP   5. PHPUnit_Framework_TestSuite->run() /home/vagrant/RH/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:440
PHP   6. PHPUnit_Framework_TestSuite->run() /home/vagrant/RH/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
PHP   7. PHPUnit_Framework_TestCase->run() /home/vagrant/RH/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
PHP   8. PHPUnit_Framework_TestResult->run() /home/vagrant/RH/vendor/phpunit/phpunit/src/Framework/TestCase.php:724
PHP   9. PHPUnit_Framework_TestCase->runBare() /home/vagrant/RH/vendor/phpunit/phpunit/src/Framework/TestResult.php:612
PHP  10. UserRegisterTest->setUp() /home/vagrant/RH/vendor/phpunit/phpunit/src/Framework/TestCase.php:764
PHP  11. factory() /home/vagrant/RH/tests/selenium/UserRegisterTest.php:14
PHP  12. app() /home/vagrant/RH/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:371

Fatal error: Call to a member function make() on null in /home/vagrant/RH/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php on line 62

Call Stack:
    0.0007     229664   1. {main}() /home/vagrant/RH/vendor/phpunit/phpunit/phpunit:0
    0.3939    2354648   2. PHPUnit_TextUI_Command::main() /home/vagrant/RH/vendor/phpunit/phpunit/phpunit:47
    0.3940    2355272   3. PHPUnit_TextUI_Command->run() /home/vagrant/RH/vendor/phpunit/phpunit/src/TextUI/Command.php:100
    4.4996    6652288   4. PHPUnit_TextUI_TestRunner->doRun() /home/vagrant/RH/vendor/phpunit/phpunit/src/TextUI/Command.php:149
    4.5334    6871768   5. PHPUnit_Framework_TestSuite->run() /home/vagrant/RH/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:440
    4.5514    6887848   6. PHPUnit_Framework_TestSuite->run() /home/vagrant/RH/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
    4.5555    6892152   7. PHPUnit_Framework_TestCase->run() /home/vagrant/RH/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
    4.5556    6893792   8. PHPUnit_Framework_TestResult->run() /home/vagrant/RH/vendor/phpunit/phpunit/src/Framework/TestCase.php:724
    4.6299    6963232   9. PHPUnit_Framework_TestCase->runBare() /home/vagrant/RH/vendor/phpunit/phpunit/src/Framework/TestResult.php:612
    4.6300    6981248  10. UserRegisterTest->setUp() /home/vagrant/RH/vendor/phpunit/phpunit/src/Framework/TestCase.php:764
    4.6300    6981448  11. factory() /home/vagrant/RH/tests/selenium/UserRegisterTest.php:14
    4.6300    6981568  12. app() /home/vagrant/RH/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:371    

UserRegisterTest.php

/**
 *
 * @test
 * Register a new user with partial information
 *
 * @return void
 */
public function capture_new_user_with_partial_information()
{
    $this->person = factory(Person::class)->create(); // Gives me an error 
}

ModelFactory.php

$factory->define(RH\Person::class, function (Faker\Generator $faker) {
return [
    'nombre' => $faker->firstName,
    'apellido_paterno' => $faker->lastName,
    'apellido_materno' => $faker->lastName,
    // There is more fields

];
});

我知道我可以以静态方式进行,但我觉得使用随机数据进行测试会更好。

【问题讨论】:

  • uyyy,粘贴失败!现在,我更新了问题
  • 试试这个:$user = factory(Person::class)->create(); 有什么错误吗?
  • 还有factory(RH\Person::class)->create()?还是factory(\RH\Person::class)->create()
  • 好吧,我很难在没有看到您的代码的情况下说出来,但您遇到的错误是因为您从未实例化 laravel 应用程序实例。这发生在 laravel 提供的 TestCase.php 中。如果你想使用集成,我相信你需要让你的测试扩展 TestCase,并让 TestCase 扩展集成类

标签: php laravel phpunit


【解决方案1】:

您收到错误 Call to a member function make() on null,因为您从未实例化 Laravel 应用程序容器的实例。当你的入口点是通过测试时,如果你要依赖它,你需要确保实例化它。

因此,laravel 提供了一个 TestCase.php ,其中包含一个方法 createApplication() 为您完成此任务。

您需要让您的测试扩展此类,以便parent::setUp() 方法将实例化一个应用程序实例。

在您的情况下,您正在尝试将 Selenium 与 Integrated 软件包一起使用。您可以做的是让您的测试类扩展 TestCase 并将您的 TestCase.php 的开头更改为:

<?php

use Laracasts\Integrated\Extensions\Selenium;

class TestCase extends Selenium
{
     // leave whatever else was here
}

这应该可以解决您的问题。

如果您有多个测试套件想要使用不同的包或类似的东西,那么创建多个 TestCase 类或类似的东西可能是个好主意,您可以在这部分工作......; p(只需将新的 TestCase 添加到您的 composer.json)

【讨论】:

  • 解决方案是按照你说的将TestCase复制到SeleniumTestCase!
猜你喜欢
  • 2015-09-16
  • 1970-01-01
  • 2020-12-28
  • 2018-04-18
  • 2020-02-26
  • 2015-12-02
  • 2017-03-13
  • 1970-01-01
  • 2020-11-01
相关资源
最近更新 更多