swoft-cloud / swoft

🚀 PHP Microservice Full Coroutine Framework
https://swoft.org
Apache License 2.0
5.58k stars 788 forks source link

跑单元测试时,如果被测试接口有使用RPC,则会报错 #389

Closed usjstb closed 5 years ago

usjstb commented 6 years ago
Q A
Bug report? yes
Feature request? yes/no
Swoft version dev-master (d7a44f3)
Swoole version 2.1.3
PHP version 7.1.17
Runtime environment Docker

Details

跑单元测试时,报错:


[{"msg":"Return value of Swoft\\App::getAppProperties() must be an instance of Swoft\\Core\\Config, null returned","file":"\/var\/www\/swoft\/vendor\/swoft\/component\/src\/framework\/src\/App.php","line":164,"code":0}].
Failed asserting that false is true.

/var/www/swoft/vendor/swoft/component/src/http-message/src/Testing/Base/ResponseAssertTrait.php:199 /var/www/swoft/test/Cases/bill/BillTest.php:20


> 原因是测试的接口中调用了Service

```php
/**
     * @Reference(name="billData",pool="bill",breaker="bill",version="1.0.0")
     * @var BillDataInterface
     */
    private $billDataService;

    /**
     * @RequestMapping(route="/bill_data/help",method={RequestMethod::GET, RequestMethod::POST})
     * @param Request $request
     * @return array
     */
    public function help(Request $request)
    {
        return $this->billDataService->get([],[]);
    }

接口不调用Service就可以测试通过。

之前使用非dev版本时,是另一种报错,看到有解决 https://github.com/swoft-cloud/swoft-component/pull/155
就升级到dev版本,但发现出现了新的这个报错。

huangzhhui commented 6 years ago

@usjstb Swoft\\App::getAppProperties() must be an instance of Swoft\\Core\\Config, null returned,从报错上来看是你的配置没能返回

usjstb commented 6 years ago

@usjstb Swoft\\App::getAppProperties() must be an instance of Swoft\\Core\\Config, null returned,从报错上来看是你的配置没能返回

我直接访问写的这个接口时,是正常运行的;但是在单元测试中就出现了这种问题。 是单元测试中,以命令行方式运行时配置加载方式有什么不同吗?需要对配置文件做怎样的修改吗?

huangzhhui commented 6 years ago

单元测试的不会加载 ./config 内的配置的,只会加载 ./test/config 内的

stelin commented 5 years ago

@usjstb 2.0已发布,建议升级到2.0