无法在 PHPUnit 中使用 Factory / Laravel 5.1

Imposible to use Factory in PHPUnit / Laravel 5.1

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

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

事情是当我尝试用 Faker 生成一个人时,我收到这个错误消息:

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

];
});

我知道我可以用静态方式来做,但我觉得用随机数据测试更好。

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

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

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

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

<?php

use Laracasts\Integrated\Extensions\Selenium;

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

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

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