我正在尝试使用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
];
});
我知道我可以用静态的方式来做,但我觉得用随机数据进行测试会更好。
答案 0 :(得分: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中)