Laravel: `App::runningUnitTests()` 在运行单元测试时返回 false

huangapple go评论59阅读模式
英文:

Laravel: App::runningUnitTests() returns false while running unit tests

问题

Laravel 5上运行单元测试时,正如问题所解释的那样,App::runningUnitTests()在单元测试运行期间返回false。为什么会发生这种情况?

我的phpunit.xml文件包含以下内容:

<env name="APP_ENV" value="testing"/>

我的PHP版本是7.1

英文:

I am running unit tests on Laravel 5 and as the questions explains itself, App::runningUnitTests() is returning false during unit tests run. Why is that happening?

My phpunit.xml has this:

&lt;env name=&quot;APP_ENV&quot; value=&quot;testing&quot;/&gt;

My PHP version is 7.1.

答案1

得分: 2

The App::runningUnitTests() 方法在 Laravel 5 中,如果当前环境设置为测试环境,将返回 true。通常在 phpunit.xml 文件中,使用 APP_ENV 环境变量进行设置。在您的情况下,您已将此变量设置为 testing,因此在运行单元测试时应返回 true。

如果在运行单元测试时仍然返回 false,可能有以下几个原因:

  1. 您的测试实际上没有在测试环境中运行。请仔细检查您的测试类是否扩展了 TestCase,并确保您使用了 --env=testing 选项来运行测试。

  2. 您正在使用缓存的配置文件。Laravel 为了性能而缓存配置文件,只有在运行 php artisan config:clear 时才会清除此缓存。如果您自上次清除缓存以来对配置文件进行了更改,可能会使用过时的配置值来运行测试。

  3. 您有自定义的 App::runningUnitTests() 实现。如果您覆盖了 App 类中的 runningUnitTests 方法,您的自定义实现可能会导致意外行为。

  4. 您的 Laravel 安装中存在错误或配置错误。虽然不太可能,但可能是您的 Laravel 安装中的某些设置不正确。尝试创建一个新的 Laravel 项目,看看是否出现相同的行为。

无论如何,我建议在测试运行期间检查环境变量,以确保 APP_ENV 已设置为 testing。您可以使用 getenv('APP_ENV') 或 $_ENV['APP_ENV'] 来进行检查。如果仍然遇到问题,请尝试使用 --debug 选项运行测试,以查看是否可以识别任何问题。

英文:

The App::runningUnitTests() method in Laravel 5 returns true if the current environment is set to testing. This is usually set in the phpunit.xml file using the APP_ENV environment variable. In your case, you have set this variable to testing, so it should be returning true during unit tests run.

If you're still getting false when running unit tests, there could be a few reasons why this is happening:

Your test is not actually running in the testing environment. Double-check that your test class extends TestCase and that you're running your tests with the --env=testing option.

You're using a cached configuration file. Laravel caches the configuration files for performance, and this cache is only cleared when you run php artisan config:clear. If you've made changes to your configuration files since the last time you cleared the cache, you may be running your tests with outdated configuration values.

You have a custom App::runningUnitTests() implementation. If you've overwritten the runningUnitTests method in the App class, your custom implementation may be causing the unexpected behavior.

There is a bug or misconfiguration in your Laravel installation. This is unlikely, but it's possible that something is not set up correctly in your Laravel installation. Try creating a new Laravel project and see if you get the same behavior.

In any case, I recommend checking the environment variables during the test run to make sure that APP_ENV is set to testing. You can do this by using getenv('APP_ENV') or $_ENV['APP_ENV']. If you're still having trouble, try running your tests with the --debug option to see if you can identify any issues.

答案2

得分: 0

将以下内容添加到phpunit.xml文件中:

<server name="APP_ENV" value="testing"/>
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
英文:

add the following lines to phpunit.xml file:

&lt;server name=&quot;APP_ENV&quot; value=&quot;testing&quot;/&gt;
&lt;server name=&quot;DB_CONNECTION&quot; value=&quot;sqlite&quot;/&gt;
&lt;server name=&quot;DB_DATABASE&quot; value=&quot;:memory:&quot;/&gt;

答案3

得分: 0

php artisan config:clear解决了问题,原来是配置缓存的问题。之前我尝试了php artisan config:cache

英文:

As it turns out config cache was the issue. php artisan config:clear did the trick. Earlier, I was trying php artisan config:cache.

huangapple
  • 本文由 发表于 2023年3月7日 15:49:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定