【发布时间】: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 扩展集成类