Behat 3 and Laravel, testing environment not being honoured

本小妞迷上赌 提交于 2019-12-04 15:50:42

With PHPUnit you can configure the environment in a bootstrap and that configuration will be used throughout the entire testing. With Behat you have a different situation, unlike PHPUnit, it makes requests to a server and doesn't talk to your Php code directly. Though to us, developers, it seems like we are testing the code that sits in the next folder to our features, in reality it is accessed via our local server, which points to that code. In other words you are dealing with a remote code that has zero exposure to your tests and ignores (as expected) the configuration (which you are setting on your local code anyway).

So, to pass any configuration to your code you need to think about your server as the entry point. There are two basic approaches – use two local domains for your app (app.dev and app.tst), or make your app.tst accept some parameter / cookie that will tell it that it's under the test. The first would be the best option as you are not adding any logic to your app and can setup environment variables via server config or make the domain point to a different bootstrap, etc.

In your FeatureContext.php add this method:

/**
 * @BeforeSuite
 */
 public static function bootstrapLaravel()
 {
    $unitTesting = true;
    $testEnvironment = 'testing';


    require __DIR__ . '/../../../bootstrap/start.php'; // adjust this to right path. This will work if FeatureContext.php is in app/tests/acceptance
 }

This will use the testing database and include Laravel aswell (so you can use your models as well).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!