![](/img/trans.png)
[英]Laravel package unit testing failing for helper function in config file (call to undefined method base_path())
[英]Unit testing with the config app file in Laravel
我的模型方法依赖于config()
全局,这里;
public function getGroup()
{
if(config('app.pages.'.$this->group.'.0')) {
return $this->group;
}
return "city";
}
我试图在我的单元测试类中测试这个方法,
public function testGetGroupReturnsCityAsDefault()
{
$response = new Response();
$response->group = "town";
$test = $response->getGroup();
dd($test);
}
我得到的错误是;
Error: Call to a member function make() on null
/home/vagrant/sites/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:62
/home/vagrant/sites/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:163
我知道这与config()全局有关。 但不确定如何在我的测试中设置它。 我试过了
public function setUp()
{
config(['app.pages' => [
'city' => [........
但得到了同样的错误。 我怎么设置它?
我不确定你是否已经解决了这个问题,但我遇到了类似的问题。
为了让它发挥作用,我必须做两件事:
1)确保您正在调用setUp父方法,如下所示:
public function setUp()
{
parent::setUp();
// other stuff
}
2)确保您的测试类扩展Laravel TestCase
而不是PHPUnit_Framework_TestCase
简要说明 :基本上你得到的错误是因为Laravel的ContainerInstance
为null,因为你正在浏览PHPUnit,因此它从未创建过。 如果执行上述步骤,您将确保Laravel首先实例化容器实例。
PS如果你最终要最终引用env
变量,你应该查看phpunit.xml
部分的环境变量。
Laravel中的PHP单元测试实现
1)在你的系统中安装php单元
composer global require phpunit/phpunit *
- Add this configuration in below path in D:\wamp64\bin\php\php7.3.5\php.ini
[xdebug]
zend_extension="d:/wamp64/bin/php/php7.3.5/zend_ext/php_xdebug-2.7.2-7.3-vc15-x86_64.dll"
xdebug.remote_enable = on
xdebug.profiler_enable = off
xdebug.profiler_enable_trigger = Off
xdebug.profiler_output_name = cachegrind.out.%t.%p
xdebug.profiler_output_dir ="d:/wamp64/tmp"
xdebug.show_local_vars=0
xdebug.remote_autostart=On
2)签入CMD - phpunit - 创建.env.testing并创建新的测试数据库 - 为虚拟数据创建Faker - 编写测试用例逻辑
3)phpunit file_path --filter FunctionName
4)phpunit --coverage-html报告/
phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="APIs">
<directory suffix="Test.php">./tests/APIs</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./report"
lowUpperBound="50" highLowerBound="80" />
</logging>
<php>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="MAIL_DRIVER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
</php>
</phpunit>
ExampleTest.php
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$this->assertTrue(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.